Gigi
Gigi

Reputation: 359

Android custom migration Sqlcipher from 3 to 4

I upgraded Sqlcipher for Android from 3.5.7 to 4.1.3 in my app.

For the existing database, which has been created with Sqlcipher 3, I need a custom migration as it is based on custom parameters, as also the option 3 of this article explains.

I'm getting this exception when I try to open the database.

net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
    at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)

This is my code:

        SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
            public void preKey(SQLiteDatabase database) {
                database.rawExecSQL("PRAGMA kdf_iter=1000;");
                database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
                database.rawExecSQL("PRAGMA cipher_page_size = 4096;");
            }

            public void postKey(SQLiteDatabase database) {
                database.rawExecSQL("PRAGMA cipher_compatibility=3;");
            }
        };

        // this line generate the exception
        SQLiteDatabase database = SQLiteDatabase.openDatabase(oldDatabaseFile.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE, mHook);
        ...
        // migration code with ATTACH, sqlcipher_export etc... 
        ...

The file exists and the password is correct: The same piece of code works if I downgrade the Sqlcipher library. What I am doing wrong?

Upvotes: 3

Views: 1036

Answers (1)

Gigi
Gigi

Reputation: 359

I found the error:

I created the database with sqlchipher 3 using the parameter cipher_page_size = 4096, but it is not accepted as the default value was used.

Trying now to migrate, specifying the parameter I thought was used, it didn't work. I just had to remove this parameter and put everything in postKey method

private final SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
    public void preKey(SQLiteDatabase database) {
    }

    public void postKey(SQLiteDatabase database) {
        database.rawExecSQL("PRAGMA cipher_compatibility=3;");
        database.rawExecSQL("PRAGMA kdf_iter=1000;");
        database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
    }
};

Upvotes: 2

Related Questions