データベースの暗号化に SQLCipher を使用する Android アプリケーションがあります。アプリケーションが稼働し、多くのアクティブ ユーザーがいます。ユーザーのデータを失うことなく、アプリケーションの既存のデータベースから SQLCipher 暗号化を削除できるソリューションを探しています。
この投稿に記載されているのとは逆のことを試みましたが、暗号化されたデータベース ファイルを開くことができませんでした。
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();
}
}
そして、これが私が得たエラーログです...
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