【发布时间】:2015-07-22 18:50:32
【问题描述】:
我有一个使用 SQLCipher 进行数据库加密的 Android 应用程序。该应用程序已上线并拥有许多活跃用户。我正在寻找一种解决方案,可以在不丢失用户数据的情况下从应用程序的现有数据库中删除 SQLCipher 加密。
我尝试与this 帖子中提到的相反,但无法打开我的加密数据库文件。
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
【问题讨论】:
-
创建一个普通的SQLite数据库,打开SQLCipher数据库,将SQLCipher数据库中的数据复制到SQLite数据库中。这将与我展示的
encrypt()方法相反 in this Stack Overflow answer。 -
如果你只是想解密数据库,并继续使用 sqlcipher lib,我认为你可以使用空的新密码
rekey数据库,但如果你想移动到 android-integrated sqlite您必须采取将数据从旧数据库迁移到新数据库的方法。
标签: android encryption android-sqlite sqlcipher-android