我的android应用程序中有一个room数据库,在appclass中,我有一些逻辑来检查数据库加密状态,以便处理一些可能需要对数据库执行的加密操作。但是,每当我在加密的数据库上调用sqlcipherutils.getdatabasestate时,它就会抛出一个sqliteexception。但有趣的是,从getdatabasestate返回的状态不是空的,并且工作得非常好,并且围绕getdatabasestate调用的try-catch块没有捕获异常。因此,从我所知,异常不会影响其他逻辑。然而,每当应用程序在数据库加密后启动时,这个错误就会出现在logcat中,这令人沮丧。所以我的问题是,这个错误实际上是一个问题吗?如果是的话,我该如何解决,如果不是,我该如何消除错误?
应用程序类中的数据库逻辑(在第一行引发异常):
try {
// Get database state
SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(getApplicationContext(), DB_NAME);
// Check whether database is encrypted
if (state == SQLCipherUtils.State.UNENCRYPTED) {
// If it's unencrypted, encrypt the database
try {
// Get database instance
FinancesDatabase db = FinancesDatabase.getDatabase(getApplicationContext());
// Close database
db.close();
// Encrypt database with encryption key
SQLCipherUtils.encrypt(getApplicationContext(), DB_NAME, sharedPrefs.getEncryptKey());
Timber.i("Successfully encrypted database!");
} catch (IOException e) {
Timber.e(e, "Failed to encrypt previously unencrypted database!");
}
} else if (state == SQLCipherUtils.State.ENCRYPTED)
// Otherwise, good to go
Timber.i("Database is encrypted. No action necessary.");
else if (state == SQLCipherUtils.State.DOES_NOT_EXIST)
// No database found. Normal if first launch
Timber.w("No database found.");
} catch(Exception e) {
Timber.e(e, "Failed to get database encryption state!");
}
例外情况:
E/Database: file is not a database: , while compiling: select count(*) from sqlite_master;
net.sqlcipher.database.SQLiteException: file is not a database: , while compiling: select count(*) from sqlite_master;
at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:89)
at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:48)
at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:60)
at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1867)
at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1785)
at net.sqlcipher.database.SQLiteDatabase.keyDatabase(SQLiteDatabase.java:2486)
at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2415)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1149)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1116)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1065)
at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1019)
at com.commonsware.cwac.saferoom.SQLCipherUtils.getDatabaseState(SQLCipherUtils.java:62)
at com.commonsware.cwac.saferoom.SQLCipherUtils.getDatabaseState(SQLCipherUtils.java:46)
at edu.usm.cs.csc414.pocketfinances.AppClass.onCreate(AppClass.java:118)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6056)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
库中的SqlCipherUtils.GetDatabaseState实现:
public static State getDatabaseState(Context ctxt, String dbName) {
SQLiteDatabase.loadLibs(ctxt);
return(getDatabaseState(ctxt.getDatabasePath(dbName)));
}
public static State getDatabaseState(File dbPath) {
if (dbPath.exists()) {
SQLiteDatabase db=null;
try {
db=
SQLiteDatabase.openDatabase(dbPath.getAbsolutePath(), "",
null, SQLiteDatabase.OPEN_READONLY);
db.getVersion();
return(State.UNENCRYPTED);
}
catch (Exception e) {
return(State.ENCRYPTED);
}
finally {
if (db != null) {
db.close();
}
}
}
return(State.DOES_NOT_EXIST);
}
最佳答案
直接记录到by SQLCipher for Android。它将在任何时候记录一个无效的密码短语被使用。getDatabaseState()
尝试以空字符串作为密码短语打开数据库,因为这是未加密数据库的“密码短语”。
避免此日志消息的选项包括:
不要在android上使用sqlcipher,或者
不要使用getDatabaseState()
,还要确保用户从不提供无效的密码短语
关于android - SQLCipherUtils.getDatabaseState中具有Room Persistence Library异常的SQLCipher,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53452700/