SQLCipher Android没有使用不正确的密码

时间:2017-11-23 06:15:30

标签: android sqlite android-sqlite sqlcipher sqlcipher-android

我正在使用SQLCipher v3.5.7并在SQLiteDatabase中发现了一个意外的行为,密码错误。

  1. 我用" key1"加密了数据库。
  2. 关闭数据库连接。
  3. 然后我尝试使用" key2"打开我的数据库,SQLiteDatabase没有抛出异常。相反,它将旧密码(key1)更新为新密码(key2)。我通过在SQLiteBrowser中打开.db文件来验证这一点。
  4. 有人能帮我解释为什么会这样吗?

    private static SQLiteCipherDatabaseHelper createDBConnection(Context context, String databasePath, final String key) throws SQLiteDatabaseException {
        if (dbInstance == null) {
            dbInstance = new SQLiteCipherDatabaseHelper(context, databasePath);
    
            String path = context.getDatabasePath(databasePath).getPath();
            File dbPathFile = new File(path);
            if (!dbPathFile.exists()) {
                   dbPathFile.getParentFile().mkdirs();
            }
    
            setDatabaseWithDBEncryption(key);
        }
        return dbInstance;
    }
    
    private static void setDatabaseWithDBEncryption(String encryptionKey) throws SQLiteDatabaseException {
        loadSQLCipherLibs();
        try {
            sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(new File(context.getDatabasePath(databasePath).getPath()), encryptionKey, null);
        } catch (Exception e) {
            SyncLogger.getSharedInstance().logFatal("SQLiteCipherDatabaseHelper", "Failed to open or create database. Please provide a valid encryption key");
            throw new SQLiteDatabaseException(SyncErrorCodes.EC_DB_SQLCIPHER_FAILED_TO_OPEN_OR_CREATE_DATABASE, SyncErrorDomains.ED_OFFLINE_OBJECTS, SyncErrorMessages.EM_DB_SQLCIPHER_FAILED_TO_OPEN_OR_CREATE_DATABASE, e);
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您是否升级了db版本?

 private static final int DATABASE_VERSION = 2;//from 1 to 2
 private static class OpenHelper extends SQLiteOpenHelper {
    OpenHelper(Context context) // constructor
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
 {
     //Changes in db mentioned here
 }
}