6

Hi in my running app i already have an existing sqlite database. And now the problem is anybody can pull the sqlite database from device and can be use it. Now i need to encrypt the sqlite file. I found that SQLCipher is used to encrypt the sqlite database. But the real problem is i don't have any idea with SQLCipher and don't know how it works. Try to use different projects. Nothing help. Please anyone tell me how to encrypt my sqlite database.

Thanks in advance.

Amsheer
  • 7,046
  • 8
  • 47
  • 81

1 Answers1

19

Step #0: Add the code to your UI to prompt the user to enter a passphrase.

Step #1: Download the SQLCipher for Android ZIP file.

Step #2: UnZIP the ZIP file and navigate to the directory that has an assets/ and a libs/ folder.

Step #3: Copy the contents of the assets/ directory into your project's assets/ directory.

Step #4: Copy the contents of the libs/ directory into your project's libs/ directory. Gradle/Android Studio users will also need to add a line to the top-level dependencies closure loading up the contents of libs/, if you do not have one already.

Step #5: Replace all relevant android.database.* and android.database.sqlite.* imports with their SQLCipher for Android equivalents. If you are using an IDE that can help you resolve missing imports (e.g., Ctrl-Shift-O in Eclipse), the easiest thing to do is to get rid of all existing android.database.* and android.database.sqlite.* imports and let the IDE help resolve them. Choose the net.sqlcipher imports when given the choice.

Step #6: You will now have compiler errors on a few methods where you open a database (e.g., getReadableDatabase() on SQLiteOpenHelper), where you now need to pass in the passphrase you collected from the user in Step #0.

This will work for new apps starting up with new databases. There is additional work involved to upgrade an existing app with existing users, if you want to allow those users to switch to an encrypted database.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks it works. Now i've an another issue i need to encrypt the old sqlite database to db using SQLCipher how to do it? I try something that is from http://sqlcipher.net/sqlcipher-api/ (Example 1). I don't know how o do that. Please help me. – Amsheer Jul 24 '14 at 06:41
  • @Amsheer: Please ask a separate Stack Overflow question for that. – CommonsWare Jul 24 '14 at 10:19
  • @CommonsWare This is a perfect tutorial. How can I get SQLCIpher working on an already existing unencrypted database? What should be the best approach? – Aritra Roy Apr 24 '15 at 16:30
  • 1
    @Aritra: To encrypt an existing database, use the `encrypt()` method from [this source file](https://github.com/commonsguy/cwac-loaderex/blob/master/src/com/commonsware/cwac/loaderex/SQLCipherUtils.java). If you have additional questions in this area, please ask a fresh Stack Overflow question. – CommonsWare Apr 24 '15 at 23:30
  • @CommonsWare Thanks. Here is a new detailed question of my problems, http://stackoverflow.com/questions/29860873/using-sqlcipher-with-android It would be awesome if you can help me on it. – Aritra Roy Apr 25 '15 at 04:05
  • 3
    Dude @CommonsWare don't forget to add SQLiteDatabase.loadLibs(this); – Adolf Dsilva Jul 26 '15 at 18:26
  • Downloaded the `zip` file, there is no `assets/` nor `lib/` folders. – A-Sharabiani Sep 25 '16 at 23:48
  • 1
    @AliSharabiani: I don't know if they distribute the library that way anymore. It ships as an AAR for use with Gradle and Android Studio. – CommonsWare Sep 25 '16 at 23:52