Upgrading to SQLCipher 4

A post was split to a new topic: Issue Migrating From android-database-sqlcipher:3.5.9 to sqlcipher-android:4.5.5

Hi @developernotes ,

Iā€™m trying to migrate db from net.zetetic:android-database-sqlcipher:3.5.9 to net.zetetic:sqlcipher-android:4.5.7 and the hook in the latter is providing SQLiteConnection instead of SQLiteDatabase in preKey() and postKey().

Could you help me on getting the database in postKey and handling the migration?

Hi @Gokulnath,

Here [1] is an example of migrating a SQLCipher database to the latest version using the sqlcipher-android library.


  1. sqlcipher-android/sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/MigrateDatabaseFrom1xFormatToCurrentFormatTest.java at master Ā· sqlcipher/sqlcipher-android Ā· GitHub ā†©ļøŽ

Is " Option 2: Backwards Compatibility" would work nowadays for migrating from net.zetetic:android-database-sqlcipher:3.5.7 to net.zetetic:android-database-sqlcipher:4.5.4?

Hi @FrozenPyrozen,

Yes, you could use the compatibility PRAGMA to upgrade your library to 4.5.4 and access a SQLCipher 3.x database.

1 Like

Iā€™m not a native android dev so I wanted to check should I use it like this?

// DatabaseHelper.java
 @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        // Apply PRAGMAs to maintain SQLCipher 3 compatibility
        db.execSQL("PRAGMA cipher_compatibility = 3;");
        db.execSQL("PRAGMA cipher_page_size = 1024;");
        db.execSQL("PRAGMA kdf_iter = 64000;");
        db.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;");
        db.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;");
    }

Hi @FrozenPyrozen,

No, if you are using the android-database-sqlcipher library, you will want to use the SQLiteDatabaseHook and set your compatibility via the postKey event:

SQLiteDatabase hook = new SQLiteDatabaseHook() {
  @Override
  public void preKey(SQLiteDatabase database) {}
  @Override
  public void postKey(SQLiteDatabase database) {
    database.execSQL("PRAGMA cipher_compatibility = 3;");
  }
};

You can then provide the hook parameter as an argument when opening the database connection. This will guarantee that the compatibility setting is in place before key derivation or any other SQL operation occurs.

1 Like

Thanks, how to understand where to place a hook?

Hi @FrozenPyrozen,

The hook can be provided to the static openDatabase/openOrCreateDatabase methods found on the SQLiteDatabase class, or if youā€™re using a subclass to the SQLiteOpenHelper.

1 Like

I was looking into MigrateDatabaseFrom3xFormatToCurrentFormatTest as an example, and Iā€™ve seen some issues with SQLiteConnection on my Android studio so Iā€™ve created a helper method

public static SQLiteDatabaseHook migrateDbFromSupportHook() {
        return new SQLiteDatabaseHook() {
            @Override
            public void preKey(SQLiteDatabase database) {}

            @Override
            public void postKey(SQLiteDatabase database) {
                // Apply PRAGMAs to maintain SQLCipher 3 compatibility for net.zetetic:android-database-sqlcipher after 4x version
                database.execSQL("PRAGMA cipher_compatibility = 3;");
                database.execSQL("PRAGMA cipher_page_size = 1024;");
                database.execSQL("PRAGMA kdf_iter = 64000;");
                database.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;");
                database.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;");
            }
        };

    }

and passing this hook into every openDatabase method calling in project. Is this okay?

Hi @FrozenPyrozen,

Once you set the compatibility version you donā€™t need to set the other pragma items as they are set on your behalf. See the implementation for reference.

Iā€™ve got dew questions:

  1. Everything above, but only with database.execSQL("PRAGMA cipher_compatibility = 3;"); would be enough?
  2. Iā€™ve got import net.sqlcipher.database.SQLiteException; and error ā€œCannot resolve symbol ā€˜SQLiteExceptionā€™ā€. Does SQLiteException moved somewhere in newer versions?

Correct.

In the SQLCipher for Android 4.5.4 release [1], Android Database SQLite Exception classes are now used for compatibility with Android Support and Room.


  1. SQLCipher 4.5.4 Release | Zetetic ā†©ļøŽ

1 Like

Where I could find an info what need to be changed there?

error: cannot access SupportSQLiteDatabase SQLiteDatabase.loadLibs(context);

SQLiteDatabase imported from import net.sqlcipher.database.SQLiteDatabase;

This error is not visible on Android studio, only on CI/CD when creating build

Hi @FrozenPyrozen,

That sounds like a project configuration issue, or possibly your environment. SupportSQLiteDatabase is not part of SQLCipher for Android [1].


  1. SupportSQLiteDatabase  |  Android Developers ā†©ļøŽ