File is encrypted or is not a database after upgrade SQLCipher from v.2.2.2.0 to v.3.1.0

I used SQLCipher v.2.2.0 for my android application, and upgraded to SQLCipher v.3.1.0.
When I updated a device on which old version was installed to new apk with SQLCipher v.3.1.0, the following crash occurred.

sqlite returned: error code = 26, msg = file is encrypted or is not a database
CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2092)
at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1958)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:875)
at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:907)
at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:197)
at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:184)

I downloaded the library, https://s3.amazonaws.com/sqlcipher/3.2.0/sqlcipher-for-android-community-v3.2.0.zip.
I encrypted database by using SQLiteOpenHelper.getReadableDatabase(String password), getWritableDatabase(String password)…
Do I have to build library after changing option?
Or do I have to change encryption method?

Thank you.

Hi @sunach

The SQLCipher 3.x library has an increased key derivation length from what was used in the 2.x library. It went from 4,000 to 64,000 which is likely the cause of the error you are seeing when attempting to open the database. You will need to either update the database file format by performing a one time upgrade using a postKey hook operation, example here, or adjust the key derivation length down to 4000 using PRAGMA kdf_iter via a preKey hook. More information regarding the 3.0 release can be found here.

Hi Mr. Nick ( @developernotes ),

I’ve been developing android apps using sqlcipher for the last few years using eclipse and Android Studio.
I use sqlcipher 3.0.1 jar before i start developing to support Marshmallow apps.
Now, i’m using maven lib : compile ‘net.zetetic:android-database-sqlcipher:3.3.1-2@aar’

But when i install the apk file i always get this error :
Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: select locale failed

I try to change the PRAGMA kdf_iter using
db.execSQL(“PRAGMA cipher_default_kdf_iter = 4000;”);
but unfortunately i can’t access my db using :
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile, new String(PASSPHRASE), null);

Is there anything else i can do ?
I want to make sure the app work perfectly when they upgrade to new version

Hi @Chris_Samuel

Can you try upgrading to the latest 3.5.2 version of the software? There have been some large changes to the library since 3.3.1-2.