2

I want to encrypt an database using sqlcipher.

I have done with the integration os openssl and sqlcipher integration and the build works perfect.

But my issue is I am not able to encrypt my database. I don't know how to perform that activity or method to encrypt a database using sql cipher.

I read on the SQL Cipher but I am not able to understand the same process. I tried the code that is provided by them but not working .

EDIT: Can any one tell my how to set PRAGMA key for the same and then how to start with the encryption ? As only this part is remain for my encryption to get completed.

Please help me out from this situation.

Thanks in advance

MPelletier
  • 16,256
  • 15
  • 86
  • 137
V.V
  • 3,082
  • 7
  • 49
  • 83
  • I just went through the encryption export bullshit law paperwork (it took about a month, just because I encrypted the stupid database). When you really want to enable encryption, do the paperwork NOW and not when you are ready to submit your app, it will save you tons of time. – JustSid Apr 13 '11 at 11:54
  • you want to encrypt it inside the iPhone? or before you get from server or something? – Ahmad Kayyali Apr 13 '11 at 11:54
  • When I create my database it should be encrypted such that no other external application from MAC or windows can access the database. – V.V Apr 13 '11 at 12:03

1 Answers1

4

With SQLCipher make sure you have a brand new SQLite database. Trying to pragma the database with a key while it already has data for some reason just tries to decrypt it.

Here is some additional information on working with an existing SQLite database or here. In this example encrypted.db is that brand new database you create and pragma.

ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'secret'; -- create a new encrypted database
CREATE TABLE encrypted.t1(a,b); -- recreate the schema in the new database (you can inspect all objects using SELECT * FROM sqlite_master)
INSERT INTO encrypted.t1 SELECT * FROM t1; -- copy data from the existing tables to the new tables in the encrypted database
DETACH DATABASE encrypted;
Aryesh
  • 386
  • 2
  • 16
Joe
  • 56,979
  • 9
  • 128
  • 135
  • Hi Joe, I had tried the above command in sql command prompt and also got the success But I don't know how to use the same with xcode for development and leep my .db file encrypted using code for iphone. can you please let me know how to do that ? – V.V Apr 14 '11 at 03:58
  • What is your set up/goal? Do you have a SQLite database on your computer that is already filled with data and you want it to be already encrypted when on the iPhone? Or do you just have a schema that will that needs to be set up on the iPhone and the all of the data collected will be encrypted. – Joe Apr 14 '11 at 12:52
  • If you are using SQLCipher from iOS you'll be using the SQLite C API to interact with the database. A the end of [this tutorial](http://sqlcipher.net/documentation/ios#solution) there are some examples of how to use the API to set the password. You can find the reference documentation for [SQLite online](http://www.sqlite.org/cintro.html). Finally, you can look at a fully working iOS application using SQLCipher called [SQLCipherSpeed on GitHub](https://github.com/sjlombardo/SQLCipherSpeed). – Stephen Lombardo Apr 14 '11 at 21:53