Reading encrypted database on Android

162 views
Skip to first unread message

jwoo...@googlemail.com

unread,
Dec 23, 2013, 2:27:18 PM12/23/13
to csharp...@googlegroups.com
Hi,

I've created an encrypted database using C#-SQLite after reading that it was compatible with SQLCipher. I'm using the following command to encrypt:

PRAGMA hexkey="0x0102030405060708090A0B0C0D0E0F01";

I'm attempting to open the file using SQLCipher by creating a char array of the key and converting to String to use as the password as follows:

private static final char[] DB_KEY = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x01 };

String password = new String(MAP_KEY);

mDatabase = SQLiteDatabase.openOrCreateDatabase(dbPath, password, null);

This throws an exception that the database is corrupted or encrypted.

Firstly, I would like to confirm that the encryption is even compatible with SQLCipher as this is information I got from a StackOverflow question and if I'm barking up the wrong tree I'll move on.

If it is compatible, what am I doing wrong with the decryption?

Thanks in advance,

Jason

Stephen Lombardo

unread,
Dec 24, 2013, 9:51:21 AM12/24/13
to csharp...@googlegroups.com
Hello Jason,

I believe that Nick addressed the formatting of the hexkey already on stack overflow here http://stackoverflow.com/questions/20750060/sqlcipher-android-and-hexkey-encryption

In addition, it is my recollection that C#-SQLite ported a very old version of SQLCipher, version 1.0. The latest version of SQLCipher that is available on the website for android is SQLCipher 3, which adds a per-page MAC and increases the key derivation rounds. Thus, by default the databases are incompatible, even using the same key. To open a 1.x database on android, you'd need to use a database hook to alter the HMAC and KDF iteration settings, i.e.

  SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
    public void preKey(SQLiteDatabase database) {
      database.rawExecSQL("PRAGMA cipher_default_use_hmac = off");
      database.rawExecSQL("PRAGMA cipher_default_kdf_iter = 4000");
    }
    public void postKey(SQLiteDatabase database) {}
  }

  // then

  SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath, password, null, hook);

Cheers,
Stephen


--
You received this message because you are subscribed to the Google Groups "C#-SQLite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to csharp-sqlit...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages