Android自定义迁移Sqlcipher从3到4

标签 android database-migration sqlcipher sqlcipher-android

我在我的应用程序中将适用于 Android 的 Sqlcipher 从 3.5.7 升级到 4.1.3。

对于使用 Sqlcipher 3 创建的现有数据库,我需要自定义迁移,因为它基于自定义参数,也是 this article 的选项 3。解释。

当我尝试打开数据库时出现此异常。

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)

这是我的代码:

        SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
            public void preKey(SQLiteDatabase database) {
                database.rawExecSQL("PRAGMA kdf_iter=1000;");
                database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
                database.rawExecSQL("PRAGMA cipher_page_size = 4096;");
            }

            public void postKey(SQLiteDatabase database) {
                database.rawExecSQL("PRAGMA cipher_compatibility=3;");
            }
        };

        // this line generate the exception
        SQLiteDatabase database = SQLiteDatabase.openDatabase(oldDatabaseFile.getAbsolutePath(), password, null, SQLiteDatabase.OPEN_READWRITE, mHook);
        ...
        // migration code with ATTACH, sqlcipher_export etc... 
        ...

文件存在且密码正确:如果我降级 Sqlcipher 库,则同一段代码可以正常工作。我做错了什么?

最佳答案

我发现了错误:

我使用参数 cipher_page_size = 4096 用 sqlchipher 3 创建了数据库,但它不被接受,因为使用了默认值。

现在尝试迁移,指定我认为已使用的参数,但没有成功。我只需删除此参数并将所有内容放入 postKey 方法

private final SQLiteDatabaseHook mHook = new SQLiteDatabaseHook() {
    public void preKey(SQLiteDatabase database) {
    }

    public void postKey(SQLiteDatabase database) {
        database.rawExecSQL("PRAGMA cipher_compatibility=3;");
        database.rawExecSQL("PRAGMA kdf_iter=1000;");
        database.rawExecSQL("PRAGMA cipher_default_kdf_iter=1000;");
    }
};

关于Android自定义迁移Sqlcipher从3到4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634010/

相关文章:

linux - 在 Ubuntu 14.10 上配置 SQLCipher

encryption - 如何在命令行上解密加密的sqlcipher数据库文件?

ios - 核心数据加密类

java - 设置测试自动化框架 Selendroid 的问题

Android - 如何适应不同屏幕尺寸的布局

android - 我想获取不重复的随机数据

java - 是否可以将 Flyway、Liquibase 等数据库迁移工具与应用程序代码库集成?

java - 如何设置这个 View ?

sql-server - 如何将本地 Visual Studio mdf 数据库导出为 Azure SQL 数据库的格式

c# - Entity Framework 代码优先与 DbUp