Converting existing sqlite to encrypt SQLCipher Android

If we have an existing application which is running successfully with sqlite database package. Now we need to convert the sqlite to encrypted SQL Cipher. Simple steps to achieve that.

Step 1:                  

Add the code to your UI to prompt the user to enter a passphrase.

Step 2:

Download the SQLCipher for Android ZIP file.

Step 3:

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

Step 4:

Copy the contents of the assets/ directory into your project’s assets/ directory.

Step 5:

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 6:

 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 7: 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.

Upgrade an existing app

To update existing sqlite database Use the following methd:

public static void encrypt(Context ctxt, String dbName, String passphrase)

                     throws IOException {

              // Old sqlite file

              File originalFile = ctxt.getDatabasePath(dbName);

              // Check sqlite file existing or not

              if (originalFile.exists()) {

                     // Create neww file path

                     File newFile = File.createTempFile(“sqlcipherutils”, “tmp”,getCacheDir());

                     // Open existing database using SQLCipher

                     SQLiteDatabase db = SQLiteDatabase.openDatabase(getAbsolutePath(), “”, null,SQLiteDatabase.OPEN_READWRITE);

                     // Encrypt existing database


                                  “ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”,getAbsolutePath(), passphrase));

rawExecSQL(“SELECT sqlcipher_export(‘encrypted’)”);

rawExecSQL(“DETACH DATABASE encrypted;”);

                     // Get database version

                     int version = db.getVersion();

                    // close the database


                     // Open SQLCipher

                     db = SQLiteDatabase.openDatabase(newFile.getAbsolutePath(),

                                  passphrase, null, SQLiteDatabase.OPEN_READWRITE);

                     // Set version to database



                     // Delete origional file


                     // Rename file to rigional file





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s