I have an Android application which uses SQLCipher for database encryption. The application has gone live and has many active users. I'm looking for a solution which can remove the SQLCipher encryption from application's existing database without loosing user's data.
I tried doing the inverse of what is mentioned in this post but unable to open my encrypted database file.
public static void decrypt(Context ctxt, String dbName, String passphrase)
throws IOException {
try {
File originalFile = ctxt.getDatabasePath(dbName);
int version = 0;
if (originalFile.exists()) {
File newFile = File.createTempFile("sqlite", "tmp", ctxt.getCacheDir());
net.sqlcipher.database.SQLiteDatabase dbCipher = net.sqlcipher.database.SQLiteDatabase.openDatabase(
originalFile.getAbsolutePath(), passphrase, null,
net.sqlcipher.database.SQLiteDatabase.OPEN_READWRITE);
if (dbCipher.isOpen()) {
dbCipher.rawExecSQL(String.format(
"ATTACH DATABASE '%s' AS plaintext KEY '%s';",
newFile.getAbsolutePath(), passphrase));
dbCipher.rawExecSQL("SELECT sqlcipher_export('plaintext')");
dbCipher.rawExecSQL("DETACH DATABASE plaintext;");
version = dbCipher.getVersion();
dbCipher.close();
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(newFile, null);
db.setVersion(version);
db.close();
originalFile.delete();
newFile.renameTo(originalFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
And, here are the error logs I got...
06-04 11:33:54.929: E/SQLiteLog(12309): (26) file is encrypted or is not a database
06-04 11:33:54.929: E/DefaultDatabaseErrorHandler(12309): Corruption reported by sqlite on database: /data/data/ril.jio.protrak/cache/sqlite1817652413tmp