有没有办法在不知道加密密钥的情况下用SQLCipher验证文件是否是有效的SQLite数据库?换句话说,有没有加密的校验和或魔术字?
从this post我理解正常的SQLite 3数据库只是以字符串“sqlite 3”开头,如果我知道密码,我可以检查实际高级数据库结构using this的有效性。
错误消息“文件已加密或不是数据库”found here表明答案是否定的 - 但任何人都可以确认?
不可以。加密的数据库文件与包含随机数据的文件无法区分。
我在DBHelper的构造函数中通过此代码解决(它不是纯净的,但它可以工作......)
private DBHelper() {
SQLiteDatabase.loadLibs(context);
File dbFile = context.getDatabasePath(DB_NAME);
try {
if (dbFile.exists()) { // is db file exists?
// check if db file can be opened
SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), DB_PASSWORD, null, SQLiteDatabase.OPEN_READWRITE).close();
}
} catch (SQLiteException ex) { // it's impossible open db file - we must encrypt it
encryptDatabase(context, dbFile);
}
}