android - SQLCipher can't open database after apprestart -
what i'm trying do
in app i'm creating we're saving secret user data needs encrypted. we'd use sqlcipher. we've implemented in documentation.
now i'm facing following problem. if create database first time can read , write db. if restart app i'm unable read database again , tons of errors. doing wrong?
here's code of our sqliteopenheler:
public database (context ctx){ super(ctx, database_name, null, database_version); try{ mkeysaver = new keysaver(ctx); file databasefile = ctx.getdatabasepath(database.database_name); if(databasefile.exists()){ sqlitedatabasehook hook = new sqlitedatabasehook(){ public void prekey(sqlitedatabase database){ database.rawexecsql("pragma kdf_iter = 5000"); } public void postkey(sqlitedatabase database){} }; mdb = sqlitedatabase.openorcreatedatabase(databasefile, mkeysaver.getkey(), null, hook); } else { databasefile.mkdir(); mdb = sqlitedatabase.openorcreatedatabase(databasefile, mkeysaver.getkey(), null); mdb.setlocale(locale.german); } } catch (exception e){ log.d("database", "exception " + e); } } @override public void oncreate(sqlitedatabase sqlitedatabase) { try{ sqlitedatabase.execsql(create_table_account); sqlitedatabase.execsql(create_table_assignment_details); sqlitedatabase.execsql(create_table_assignment_overview); sqlitedatabase.execsql(create_table_assignment_upload); sqlitedatabase.execsql(create_table_assignment_user); sqlitedatabase.execsql(create_table_account); }catch (exception e){ log.e("database", "exception " + e); } } @override public void onupgrade(sqlitedatabase sqlitedatabase, int oldversion, int newversion) { if (oldversion >= newversion) { return; } // falls ein db-update stattfinden sollte code hier }
and that's how instantiate it:
sqlitedatabase.loadlibs(mcontext.getapplicationcontext()); mkeysaver = new keysaver(mcontext); mdb = new database(mcontext.getapplicationcontext()); db_password = mkeysaver.getkey(); mdatabase = mdb.getwritabledatabase(db_password);
and logcatdump:
04-23 09:00:44.723 2405-2405/ivo e/database﹕ create table android_metadata failed 04-23 09:00:44.726 2405-2405/ivo e/database﹕ failed setlocale() when constructing, closing database net.sqlcipher.database.sqliteexception: file encrypted or not database @ net.sqlcipher.database.sqlitedatabase.native_setlocale(native method) @ net.sqlcipher.database.sqlitedatabase.setlocale(sqlitedatabase.java:2092) @ net.sqlcipher.database.sqlitedatabase.<init>(sqlitedatabase.java:1958) @ net.sqlcipher.database.sqlitedatabase.opendatabase(sqlitedatabase.java:875) @ net.sqlcipher.database.sqlitedatabase.openorcreatedatabase(sqlitedatabase.java:907) @ net.sqlcipher.database.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:132) @ net.sqlcipher.database.sqliteopenhelper.getreadabledatabase(sqliteopenhelper.java:197) @ net.sqlcipher.database.sqliteopenhelper.getreadabledatabase(sqliteopenhelper.java:184) @ ivo.database.adapter.accountadapter.checkaccountpassword(accountadapter.java:79) @ ivo.activity.loginactivity.attemptlogin(loginactivity.java:168) @ ivo.activity.loginactivity$2.onclick(loginactivity.java:88) @ android.view.view.performclick(view.java:4756) @ android.view.view$performclick.run(view.java:19748) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:898) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:693) 04-23 09:00:44.730 2405-2405/swisscom.ivocore e/sqliteopenhelper﹕ couldn't open ivo.db writing (will try read-only): net.sqlcipher.database.sqliteexception: file encrypted or not database @ net.sqlcipher.database.sqlitedatabase.native_setlocale(native method) @ net.sqlcipher.database.sqlitedatabase.setlocale(sqlitedatabase.java:2092) @ net.sqlcipher.database.sqlitedatabase.<init>(sqlitedatabase.java:1958) @ net.sqlcipher.database.sqlitedatabase.opendatabase(sqlitedatabase.java:875) @ net.sqlcipher.database.sqlitedatabase.openorcreatedatabase(sqlitedatabase.java:907) @ net.sqlcipher.database.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:132) @ net.sqlcipher.database.sqliteopenhelper.getreadabledatabase(sqliteopenhelper.java:197) @ net.sqlcipher.database.sqliteopenhelper.getreadabledatabase(sqliteopenhelper.java:184) @ ivo.database.adapter.accountadapter.checkaccountpassword(accountadapter.java:79) @ ivo.activity.loginactivity.attemptlogin(loginactivity.java:168) @ ivo.activity.loginactivity$2.onclick(loginactivity.java:88) @ android.view.view.performclick(view.java:4756) @ android.view.view$performclick.run(view.java:19748) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:898) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:693)
you might need table android_metadata
(key,value)=('locale',your locale)