# Upgrading from android-database-sqlcipher to sqlcipher-android

**URL:** https://discuss.zetetic.net/t/upgrading-from-android-database-sqlcipher-to-sqlcipher-android/6741
**Category:** SQLCipher
**Created:** [September 5, 2024, 1:20pm UTC](https://discuss.zetetic.net/t/upgrading-from-android-database-sqlcipher-to-sqlcipher-android/6741 "2024-09-05T13:20:19Z")
**Posts on this page:** 1
**Showing post:** 36

<div class="post-metadata">

### Author: ![FrozenPyrozen](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/frozenpyrozen/32/2296_2.png) [@FrozenPyrozen](https://discuss.zetetic.net/u/FrozenPyrozen)
#### Post date: [November 21, 2024, 12:17pm UTC](https://discuss.zetetic.net/t/upgrading-from-android-database-sqlcipher-to-sqlcipher-android/6741/36 "2024-11-21T12:17:20Z")

</div>

Hi, @developernotes

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

Code:

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/2X/1/1683ed3883d8b7ab0c04846fc4c75ce1f672e8ef.jpeg)

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;");`

```auto
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:

 ![image](https://us1.discourse-cdn.com/flex016/uploads/zetetic/original/2X/8/86d499af2365e608a2887d6951a3377d3a824546.jpeg)

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

---

_[View the full topic](https://discuss.zetetic.net/t/upgrading-from-android-database-sqlcipher-to-sqlcipher-android/6741)._
