3

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.<init>(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

Maveňツ
  • 1
  • 12
  • 50
  • 89

2 Answers2

8

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.
Jamie Taylor
  • 4,709
  • 5
  • 44
  • 66
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I have pulled out the database from device it can be viewed by the sqlite Browser. So i come out with the conclusion that db is not encrypted neither corrupted. Here is the link of the sqlcipher https://github.com/sqlcipher/android-database-sqlcipher i am using to create a encrypted db from simple db. Thanks alot for considering me – Maveňツ Feb 27 '14 at 09:18
  • I reviewed sqlcipher doc they also suggest the same "" to set when using normal db – Maveňツ Feb 28 '14 at 05:33
  • I have the same issue. In my case, the problem was solved creating a new database with the same structure, with a password. Then, I've replaced the original asset/.db file with this new db file, so, when the system go to create the database and open this, works with the new db. Resume, my problem was that the original DB has a different password. If you database has data, you need to use [sqlcipher_export](https://github.com/sqlcipher/android-database-sqlcipher/issues/160) – amelian Aug 04 '15 at 17:56
0
  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:

< uses-permission> android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

< uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

  • Also, check that you have copied the right files needed: main/assets/icudt46l.zip libs/sqlcipher.jar src/main/jniLibs (library's .so files) – Ionut Sabau Apr 02 '15 at 11:47
  • can you please post the correct resolution with details. I am having the same problem. @maven – Mohit Mathur Jun 07 '16 at 06:03