file is encrypted or is not a database (Exception net.sqlcipher.database.SQLiteException)

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

I'm trying to use sqlcipher lib to encrypt my database from already existing database but while accessing the old database(i.e opening the db) gives this exception:

 02-27 13:12:21.231: E/AndroidRuntime(14687): FATAL EXCEPTION: main  02-27 13:12:21.231: E/AndroidRuntime(14687): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.sqlcipher/example.SQLDemoActivity}: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.access$600(ActivityThread.java:149)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.os.Handler.dispatchMessage(Handler.java:99)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.os.Looper.loop(Looper.java:153)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.main(ActivityThread.java:4987)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at java.lang.reflect.Method.invokeNative(Native Method)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at java.lang.reflect.Method.invoke(Method.java:511)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at dalvik.system.NativeStart.main(Native Method)  02-27 13:12:21.231: E/AndroidRuntime(14687): Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.native_setLocale(Native Method)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.setLocale(SQLiteDatabase.java:2102)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.(SQLiteDatabase.java:1968)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at example.SQLDemoActivity.onCreate(SQLDemoActivity.java:42)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.Activity.performCreate(Activity.java:5020)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)  02-27 13:12:21.231: E/AndroidRuntime(14687):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)  02-27 13:12:21.231: E/AndroidRuntime(14687):   ... 11 more 

since my old db is not encrypted so what should I set password in that case

I have tried with passing null in password string and also with "" but with no luck

reported issue to https://github.com/sqlcipher/android-database-sqlcipher/issues/124

回答1:

since my old db is not encrypted so what should i set password in that case

SQLCipher for Android can open an unencrypted database using "" as the password.

i have tried with passing null in password string and also with "" but no luck

Then either:

  • You have a bug in your code, or
  • The database is already encrypted with another password, or
  • The database is corrupted.


回答2:

  1. check if you're using the right import: import net.sqlcipher.database.SQLiteDatabase;

  2. load libs on start of the app like this:

SQLiteDatabase.loadLibs(this); //this = activity (usually a SplashScreen or MainActivity) db = DatabaseHandler.getInstance(this); //if it's singleton (and i suggest making it as such) db.getReadableDatabase(ConstantsHandler.DATABASE_PASSWORD); db.close(); 
  1. make sure it's not already encrypted with a password

  2. check File permissions:

android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!