-
Notifications
You must be signed in to change notification settings - Fork 575
Description
This is my onCreate() of ContentProvider class:
public void onCreate()
{
SQLiteDatabase.loadLibs(getContext());
mOpenHelper = new DBHelper(getContext());
try {
encrypt(getContext(),DATABASE_NAME,pass);
} catch (IOException e) {
e.printStackTrace();
}
}
This is my encrypt method:
public static void encrypt(Context ctxt, String dbName,String passphrase) throws IOException {
File originalFile=ctxt.getDatabasePath(dbName);
if (originalFile.exists()) {
File newFile=File.createTempFile("sqlcipherutils", "tmp",ctxt.getCacheDir());
SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),"", null,
SQLiteDatabase.OPEN_READWRITE);
db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
newFile.getAbsolutePath(), passphrase));
db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
db.rawExecSQL("DETACH DATABASE encrypted;");
int version=db.getVersion();
db.close();
db=SQLiteDatabase.openDatabase(newFile.getAbsolutePath(),passphrase, null,
SQLiteDatabase.OPEN_READWRITE);
db.setVersion(version);
db.close();
originalFile.delete();
newFile.renameTo(originalFile);
}
}
When i update my previous UNENCRYTED db with new ENCRYPTED db its working as expected . Note, my db version is the same for both .
I am facing issue when i try to update app whose db is now ENCRYPTED with the new version of app. Following is the error :
Caused by: net.sqlcipher.database.SQLiteException: file is encrypted or is not a database: , while compiling: select count() from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
at net.sqlcipher.database.SQLiteCompiledSql.(SQLiteCompiledSql.java:64)
at net.sqlcipher.database.SQLiteProgram.(SQLiteProgram.java:83)
at net.sqlcipher.database.SQLiteQuery.(SQLiteQuery.java:49)
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1787)
at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1752)
at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2427)
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2356)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1116)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1032)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986)
at com.xxxxxxxxxxxxxx.Provider.encrypt(Provider.java:98)
at com.xxxxxxxxxxxxxx.Provider.onCreate(Provider.java:84)*
at android.content.ContentProvider.attachInfo(ContentProvider.java:1656)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1627)
at android.app.ActivityThread.installProvider(ActivityThread.java:5015)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4589)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4522)
at android.app.ActivityThread.access$1500(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
It is throwing error at SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),"", null, SQLiteDatabase.OPEN_READWRITE);
where I am trying to open the encryted db by passing "" and if I pass the passphrase into it then the normal upgradation with fail(unencrypted db to encryted db update) . Please suggest what should be done .