Samarth Bali
Samarth Bali

Reputation: 41

"File is encrypted or is not a database" error is coming when trying to decrypt an encrypted database in android

I am encrypting a sqlite database in ubuntu terminal using sqlcipher version 3.7.17 and sqlite3 version 3.7.15.2. When i am trying to use this encrypted database in android, i am getting the error "File is encrypted or not a database". I am using sqlcipher version 2.2.0 in android.

Encryption code used in ubuntu terminal:
./sqlcipher plaintext.db
sqlite> ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'testkey';
sqlite> SELECT sqlcipher_export('encrypted');
sqlite> DETACH DATABASE encrypted;

Android code to decrypt:
SQLiteDatabase database= SQLiteDatabase.openOrCreateDatabase(databaseFile, testkey", null);

Any help would be appreciated!!

Upvotes: 3

Views: 7292

Answers (1)

Nick Parker
Nick Parker

Reputation: 1388

If you are using SQLCipher core 3.7.17, that is the SQLite version string which corresponds to the v2.2.1 tag of SQLCipher which will work properly with SQLCipher for Android 2.2.0. To verify your initial encryption is working properly, following the sqlcipher_export process, please do the following:

$> ./sqlcipher encrypted.db
sqlite> PRAGMA key = 'testkey';
sqlite> select * from sqlite_master;

If you get results displayed from the query of the sqlite_master table, we should rule out an application integration issue, could you try running your database within the SQLCipher for Android test suite? Please note that the test suite is targeting the latest SQLCipher for Android libraries, currently 3.0.2 and will require using a different key derivation length; 4000 was the default value in SQLCipher 2.x. You will want to copy your database file into the assets directory and then attempt opening it using the following example:

SQLiteDatabaseHook hook = new SQLiteDatabaseHook(){
  public void preKey(SQLiteDatabase database){}
  public void postKey(SQLiteDatabase database){
    database.execSQL("PRAGMA kdf_iter = 4000;");
};
ZeteticApplication.getInstance().extractAssetToDatabaseDirectory("encrypted.db");
File databaseFile = ZeteticApplication.getInstance().getDatabasePath("encrypted.db");
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, "testkey", 
                                                              null, hook);

From there you should be able to query your database. If you have a particular question, the SQLCipher Mailing List is also a good place to start.

Upvotes: 2

Related Questions