I am trying to integrate SQLCipher in one of my application and I want to take care of the unencrypted DB of the users who have already installed my app, So what I have done is, created a method (Pasted below ) which migrates the old unencrypted DB to encrypted one for the first time app is updated with SQLCipher implementation.
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(),passphrase, 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);
}
}
but while trying to open database in 4th line of the above method, app crashes with the following error message, Please help…
01-12 05:51:16.568: I/Database(4160): sqlite returned: error code = 26, msg = file is encrypted or is not a database
01-12 05:51:16.568: E/Database(4160): CREATE TABLE android_metadata failed
01-12 05:51:16.588: E/Database(4160): Failed to setLocale() when constructing, closing the database
01-12 05:51:16.588: E/Database(4160): net.sqlcipher.database.SQLiteException: file is encrypted or is not a database