Import an db-file - going back to MainActivity then cannot kind of 'access' db

Hey there,

I set up a db-file encrypted; everything works perfectly. For the user’s convinience I provide an option to backup the encrypted db-file. Suggested case: After a while the user uses another passphrase at the existing database the user is working with. Then the user thinks he/she has to get the backuped db-file and can import the exported db-file. But now assume the imported file is encrypted with another passphrase. So… that is what I coded:
I. Import/overwrite ==> really?

  • overwrite db-file

II. going back to MainActivity

  • try to open

III. check if db can be opened (in MainActivity):

  • can open (same passphrase used) ==> everything’s fine; works perfectly
  • cannot open (another passphrase is used in the imported db-file as saved ‘in the app’): ==> launch activity for typing/resetting passphrase ‘saved in app’

IV. check in ResettingPassphraseActivity (‘saved in app’) to match the db-file-passphrase

  • check… ==> correckt = could be opened => button: do so… => go back to MainActivity

V.
====> now I cannot access the db file correctly despite I am using the correct passphrase. Even if I retrieve (=log it) the passphrase right before datasource.open() I can see the new, correct and changed passphrase (which has ‘saved in the app’ and matches the one used for the exported and imported db-file).

what I tried so far (and a lot of stuff more…):
In MainActivity:

  • datasource = new Datasource(); // =some kind of overwrite - nope
  • datasource.close() and datasource.open() - nope
  • something involving ‘datasource = null’ - nope
  • something with opening not an instance (see later) - nope (slows down)

and now for the “weird” part of it:
If I “kill” the app (get it out of the recently used apps) - the imported db-file is opened with the newly (‘in app saved’) passphrase (and it’s the one I logged before) very smoothly. Looks like I do not use the correct instance of the datasource or something… any help?

There is not that much on the internet regarding this… help would be very appreciated… :-/

Some code - because… everyone here loves code - mh…

public void open(Context context) {
SQLiteDatabase.loadLibs(context);
database = dbHelper.getInstance(context).getWritableDatabase(getPassphrase());
database.disableWriteAheadLogging();
}

*getPassphrase() is the method for retrieving the passphrase from ‘inside the app’

public void close() {
dbHelper.close();
}

public void changePassphrase(String passphrase_old, String passphrase_new){
final String PRAGMA_KEY = String.format(“PRAGMA key = “%s”;”, passphrase_old);
final String PRAGMA_REKEY = String.format(“PRAGMA rekey = “%s”;”, passphrase_new);
// database.beginTransaction();
// database.rawExecSQL(“BEGIN IMMEDIATE TRANSACTION;”);
database.rawExecSQL(PRAGMA_KEY);
database.rawExecSQL(PRAGMA_REKEY);
// try{
// database.setTransactionSuccessful();
// database.endTransaction();
// } catch(Exception e) {
// Log.e(LOG_TAG,“transaction end failed” + e.getMessage());
// }
}

*i am not sure if i need transaction or not - works both ways :slightly_smiling_face:

I am working on:
java, android, api 22-29, sqlcipher 4.3.0

error-log:
‘file is not a database’

thank you!

Hi @harry

That does sound like a strange observation, is it possible there is an application logic issue with your import/overwrite logic? Can you replicate the problem by writing a test within the SQLCipher for Android test suite to reproduce it?

Also, executing the PRAGMA key operation, and the transaction are both unnecessary in your change password function.