Sudarshan
Sudarshan

Reputation: 1291

Android Sqlchipher: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database

I have using Sqlcipher for my database encryption. I am using SQLiteDatabase.openDatabase for creating, editing and read the sqlchiphered database. I could open ,read and edit properly but suddenly i got the follwing error and after that i couldnt open the database itself

12-25 17:59:07.961: E/Database(6794): SELECT locale FROM android_metadata failed
12-25 17:59:07.961: W/System.err(6794): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database
12-25 17:59:07.966: W/System.err(6794):     at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)
12-25 17:59:07.966: W/System.err(6794):     at net.sqlcipher.database.SQLiteDatabase.setLocale(Unknown Source)
12-25 17:59:07.966: W/System.err(6794):     at net.sqlcipher.database.SQLiteDatabase.<init>(Unknown Source)
12-25 17:59:07.966: W/System.err(6794):     at net.sqlcipher.database.SQLiteDatabase.<init>(Unknown Source)

i had checked for the problem but i couldnt get any useful info on this. So help on this problem to get me the solution

NOTE:

I had updated my sqlcipher and ran the app as a fresh one (Not an update to my previous app), it was wrking properply only but suddendly i got the above error and i couldnt open my db at all (till i clear data the application in application manager)

Currently i am using 2.2 version of SQLCipher

Thanks in Advance

Upvotes: 0

Views: 3060

Answers (2)

Nick Parker
Nick Parker

Reputation: 1388

From your previous comments, you have upgraded SQLCipher for Android from a previous version to the latest, is that correct? If so, what version were you previously running and what version are you running now? Are you able to access your database at all, or do you consistently see this issue. You may consider moving this question over to the SQLCipher Mailing List as it may be easier to diagnose via email.

Upvotes: 0

AggelosK
AggelosK

Reputation: 4341

The same thing has happened to me and it is because after the update of the SQLCipher library i also needed to upgrade the database from the version 1.1.x i was using to 2.2.2. See here why. To do that you need to use the SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(File dbPath, String key). The problem is that this method does not preserve the db version so we need to do that manually. So the code will be like this:

 //Open the old formatted db with cipher_use_hmac = OFF;
 SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {

     public void preKey(SQLiteDatabase database) {}

     public void postKey(SQLiteDatabase database) {
         database.rawExecSQL("PRAGMA cipher_use_hmac = OFF;");
     }
 };

 //Read the db version
 SQLiteDatabase old_db = SQLiteDatabase.openOrCreateDatabase(path, mPassword, mFactory, hook);
 int version = old_db.getVersion();
 old_db.close();

 //Do the upgrade to 2.x format
 try {
     SQLiteDatabase.upgradeDatabaseFormatFromVersion1To2(new File(path), mPassword);
 } catch (Exception e) {
     e.printStackTrace();
 }

//Manually set the db version to the updated db.
SQLiteDatabase updated_db = SQLiteDatabase.openOrCreateDatabase(path, mPassword, mFactory);
updated_db.setVersion(version);

The above code will permanently update the SQLCipher database format to 2.2.2 version. If you want to update to the new 3.x format, the process is different and you need to use the PRAGMA cipher_migrate method (See here). I avoided the 3.x format because the default KDF iteration count is 64000 (up from 4000) which was making the readings/writings to the database quite slow. The 2.2.2 version is compatible with KitKat.

Upvotes: 1

Related Questions