Search code examples
androidpragmasqlcipher

Android SQLcipher PRAGMA problems


Hey guys I am having a some problems with SQLcipher db for android The documentation is not too descriptive so I could not figure it out.

I am trying to modify the default number of iterations on sqlcipher for android, I am editing the notecipher app provided as demo app with sqlcipher, and want to increase the kdf_iter to i.e. 5000

By overriding the getWritableDatabase() method in the database helper i enter the pragma value just after the file is open with the password.

I can open and initialize the database, but I cannot re-open the db if I do a database.close() call.

whenever I close the database on the next open() call I get a :

I/Database(807): sqlite returned: error code = 26, msg = file is encrypted or is not a database
E/Database(807): CREATE TABLE android_metadata failed
E/Database(807): Failed to setLocale() when constructing, closing the database
E/Database(807): info.guardianproject.database.sqlcipher.SQLiteException: file is encrypted or is not a database

Solution

  • @Stephen answer is only partially correct, because according to the documentation:

    PRAGMA kdf_iter must be called after PRAGMA key and before the first actual database operation or it will have no effect.

    So the line:

    database.rawExecSQL("PRAGMA kdf_iter = 5000");

    Must be inserted in the postKey() method, and NOT in preKey().
    This worked for me.