我有一个使用 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