Skip to content

sqlcipher_export() and SQLiteOpenHelper conflict #71

Closed
@commonsguy

Description

@commonsguy

Your test case for sqlcipher_export() uses SQLiteDatabase directly. However, when I try using it in conjunction with your SQLiteOpenHelper, I run into some issues. Specifically, the metadata that Android stores for version info does not seem to make it over to the encrypted database. As a result, when I turn around and try to use the encrypted database with SQLiteOpenHelper, Android tries upgrading the database from version 0 to 1, running through the SQLiteOpenHelper onCreate() logic again, which should not be necessary.

Here is the encrypt-my-unencrypted-database method that I am using:

static void encrypt(Context ctxt) throws IOException {
 SQLiteDatabase.loadLibs(ctxt);

 File dbFile=ctxt.getDatabasePath(DATABASE_NAME);
 File legacyFile=ctxt.getDatabasePath(LEGACY_DATABASE_NAME);

 if (!dbFile.exists() && legacyFile.exists()) {
  SQLiteDatabase db=
      SQLiteDatabase.openOrCreateDatabase(legacyFile, "", null);

  db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
                              dbFile.getAbsolutePath(), PASSPHRASE));
  db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
  db.rawExecSQL("DETACH DATABASE encrypted;");
  db.close();
  legacyFile.delete();
 }
}

Thoughts?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions