Upgrading from android-database-sqlcipher to sqlcipher-android

Hi, @developernotes

Curently I’ve added checkCipherSettings to check before database.execSQL("PRAGMA cipher_compatibility = 3;"); configs and compare with after it

Code:

private void checkCipherSettings(SQLiteDatabase database) {
                String[] pragmas = {
                        "PRAGMA cipher_compatibility;",
                        "PRAGMA kdf_iter;",
                        "PRAGMA cipher_plaintext_header_size;",
                        "PRAGMA cipher_hmac_algorithm;",
                        "PRAGMA cipher_kdf_algorithm;",
                        "PRAGMA cipher_page_size;",
                        "PRAGMA cipher_use_hmac;",
                        "PRAGMA cipher_settings;"
                };

                try {
                    for (String pragma : pragmas) {
                        Cursor cursor = database.rawQuery(pragma, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            String result = cursor.getString(0);
                            Log.i("CipherSettings", "[KLOG]: " + pragma + " " + result);
                            cursor.close();
                        } else {
                            Log.i("CipherSettings", "[KLOG] Failed to retrieve value for " + pragma);
                        }
                    }
                } catch (Exception e) {
                    Log.e("CipherSettings", "[KLOG] Error while fetching cipher settings: " + e.getMessage(), e);
                }
            }

            @Override
            public void postKey(SQLiteDatabase database) {
                Log.i("DatabaseHelper", "[KLOG] postKey triggered for db: " + database);
                Log.i("DatabaseHelper", "[KLOG] _________ before this.checkCipherSettings for existing db __________: ");
                this.checkCipherSettings(database);
                Log.i("DatabaseHelper", "[KLOG] _________ after this.checkCipherSettings for existing db __________: ");
                Log.i("DatabaseHelper", "[KLOG] postKey before database cipher_compatibility to 3");
                // Apply PRAGMA to maintain SQLCipher 3 compatibility for net.zetetic:android-database-sqlcipher after 4x version
                database.execSQL("PRAGMA cipher_compatibility = 3;");
              

                Log.i("DatabaseHelper", "[KLOG] postKey after database cipher_compatibility to 3");
                Log.i("DatabaseHelper", "[KLOG] _________ before this.checkCipherSettings for cipher_compatibility = 3 db __________: ");
                this.checkCipherSettings(database);
                Log.i("DatabaseHelper", "[KLOG] _________ after this.checkCipherSettings for cipher_compatibility = 3 db __________: ");
}

And I’ve got some differences:

After this I’ve changed postKey to set exact same kdf_iter, cipher_hmac_algorithm, cipher_kdf_algorithm, cipher_page_size settings as it was before database.execSQL("PRAGMA cipher_compatibility = 3;");

public void postKey(SQLiteDatabase database) {
                Log.i("DatabaseHelper", "[KLOG] postKey triggered for db: " + database);
                Log.i("DatabaseHelper", "[KLOG] _________ before this.checkCipherSettings for existing db __________: ");
                this.checkCipherSettings(database);
                Log.i("DatabaseHelper", "[KLOG] _________ after this.checkCipherSettings for existing db __________: ");
                Log.i("DatabaseHelper", "[KLOG] postKey before database cipher_compatibility to 3");
                // Apply PRAGMA to maintain SQLCipher 3 compatibility for net.zetetic:android-database-sqlcipher after 4x version
                database.execSQL("PRAGMA cipher_compatibility = 3;");
                database.execSQL("PRAGMA kdf_iter = 256000;");
                database.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA512;");
                database.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;");
                database.execSQL("PRAGMA cipher_page_size = 4096;");

                Log.i("DatabaseHelper", "[KLOG] postKey after database cipher_compatibility to 3");
                Log.i("DatabaseHelper", "[KLOG] _________ before this.checkCipherSettings for cipher_compatibility = 3 db __________: ");
                this.checkCipherSettings(database);
                Log.i("DatabaseHelper", "[KLOG] _________ after this.checkCipherSettings for cipher_compatibility = 3 db __________: ");
}

Now they are equal, but I still have an error android.database.sqlite.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master; after this:

Maybe I could check existing db setup with sql cipher old library v3? Or maybe I missed some pragma that needs for opening db file