【问题标题】:Providing my android app with a DB encrypted with sqlcipher doesn't work, can't open it为我的 android 应用程序提供使用 sqlcipher 加密的数据库不起作用,无法打开它
【发布时间】:2016-01-12 04:07:45
【问题描述】:

我的 android 应用程序使用提供的数据库填充并位于 asset 文件夹中。我最近不得不开始在其中添加私人数据,所以我使用sqlcipher 对其进行加密。所以在开始之前:

  • 加密进行得很顺利,因为我可以轻松解密并读回它。这是通过sqlcipher shell 完成的。

  • 数据库没有加密,一切都像魅力一样工作

我所做的是,在我的应用程序第一次启动时,在我的手机上创建一个空白数据库,复制并粘贴其中提供的数据库的内容,然后,用户可以在他的电话,而不必每次都重新创建它(数据库很大)。事实上,如果用户已经在他的手机上拥有数据库以供进一步启动,他就不必以这种方式重新创建它。

但是对于sqlcipher,它不再起作用了。

重要提示:当使用带有非加密数据库的 sqlcipher 时(使用“”作为相关方法的参数,如 openDatabase),它也可以正常工作。

但是当我尝试使用加密数据库和密码时,我得到的是错误

file is encrypted or is not a database: create locale table failed

这发生在我使用这个创建一个空白数据库之后:

    //By calling this method and empty database will be created into the default system path
    //of your application so we are gonna be able to overwrite that database with our database.
    this.getReadableDatabase("password").close();

然后我尝试按照以下说明打开它:

//Open the database
        String myPath = DB_PATH + DATABASE_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, "password", null, SQLiteDatabase.OPEN_READWRITE);

然后发生错误。有人有想法吗?遗憾的是,我不是 android 专家,使用 db 已经很困难,但你可以猜到它现在正在进入另一个层次。欢迎任何帮助。提前致谢!

【问题讨论】:

    标签: android encryption android-sqlite sqlcipher


    【解决方案1】:

    经过长时间的搜索,终于解决了我的问题:Sqlcipher __ CREATE TABLE android_metadata failed

    错误来自我打开数据库的方式,无论您使用sqlcipher 还是sqlite,都会发生变化。

    使用sqlite打开数据库的旧方法:

    myDataBase = SQLiteDatabase.openDatabase(myPath, "password", null, SQLiteDatabase.OPEN_READWRITE);
    

    使用 sqlcipher 的新方法,带有SQLiteDatabaseHook

    SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
                public void preKey(SQLiteDatabase database) {
                }
    
                public void postKey(SQLiteDatabase database) {
                    database.rawExecSQL("PRAGMA cipher_migrate;");
    
                }
            };
    
    
            myDataBase = SQLiteDatabase.openOrCreateDatabase(myPath, "password", null, hook);
    

    【讨论】:

      猜你喜欢
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 1970-01-01
      • 2015-08-06
      • 2023-03-19
      相关资源
      最近更新 更多