从android sqlcipher移动到android sqlite数据库 [英] moving from android sqlcipher to android sqlite database

查看:368
本文介绍了从android sqlcipher移动到android sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从最近3天起,我试图将我的数据库升级到更高版本的SQLCipher库(v3.1.0)。我做了每一步,并遵循了几个教程。但是继续收到错误文件被加密或不是数据库。现在我试图移动到未加密的数据库ie。简单的sqlite数据库。
我们有办法将加密数据库移动到未加密的数据库吗?感谢提前。

From the last 3 days i am trying to upgrade my database to a higher version of SQLCipher library (v3.1.0). I did every step and followed a few tutorials too. But keep on getting the error "File is encrypted or not a Database". Now am trying to move to unencrypted database ie. simple sqlite database. Do we have a way to move to encrypted database to un-encrypted database? Thanks in advance.

这是我正在处理的代码:

This is the code i am working on:

public MyDBAdapter(Context context) {
    this.context = context;
    File dbFile = context.getDatabasePath(DATABASE_NAME);
    String dbPath = context.getDatabasePath(DATABASE_NAME).toString();

    if (dbFile.exists()) {
        try {
            SQLiteDatabase.loadLibs(context.getApplicationContext());//load SqlCipher libraries

            SQLiteDatabase db = getExistDataBaseFile(dbPath, KEY_PASSPHRASE_ENCRYPTION, dbFile);

            if (version == 1) {
                MigrateDatabaseFrom1xFormatToCurrentFormat(
                        dbFile, KEY_PASSPHRASE_ENCRYPTION);
            }
            System.out.println("Old Database found and updated.");
        } catch (Exception e) {
            System.out.println("No Old Database found");
        }
    }

    this.dbhelper = new MyDBHelper(this.context, DATABASE_NAME, null,
            DATABASE_Version);
    db = dbhelper.getWritableDatabase(KEY_PASSPHRASE_ENCRYPTION);

}


    private SQLiteDatabase getExistDataBaseFile(String FULL_DB_Path, String password, File dbFile) {// this function to open an Exist database 
        SQLiteDatabase.loadLibs(context.getApplicationContext());
        SQLiteDatabaseHook hook = new SQLiteDatabaseHook() {
            public void preKey(SQLiteDatabase database) {
                System.out.println("-----Inside preKey");
            }

            public void postKey(SQLiteDatabase database) {
                System.out.println("-----Inside postKey");
                database.rawExecSQL("PRAGMA cipher_migrate;");

            }
        };
        SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
                dbFile, "Test123", null, hook); // Exception
        return database;

    }


推荐答案

正在将您的SQLCipher库升级到最新版本,目前为3.1.0,而您之前的版本为2.x(正如上述注释中所述),您还需要升级数据库文件格式。 3.x版本发生的重大变化之一是将关键推算长度从4,000增加到了64,000。如果使用所有标准的SQLCipher配置,则升级数据库格式是直接的。我们已经包括一个新的PRAGMA调用 cipher_migrate ,将为您执行此操作。您可以在 SQLiteDatabaseHook postKey 事件中执行此操作,该事件将在您的呼叫中提供 SQLiteDatabase.openOrCreateDatabase 。一个例子可以在Android测试套件的SQLCipher中找到 here

If you are upgrading your SQLCipher library to the latest version, currently at 3.1.0, and your previous version was 2.x (as you mentioned in the comments above), you will need to upgrade the database file format as well. One of the big changes in the 3.x release was an increase in key derivation length, from 4,000 to 64,000. If you are using all of the standard SQLCipher configurations, upgrading the database format is straight forward. We have included a new PRAGMA call cipher_migrate that will perform this operation for you. You can execute this within the postKey event of the SQLiteDatabaseHook which is to be provided in your call to SQLiteDatabase.openOrCreateDatabase. An example of this can be found in the SQLCipher for Android test suite here.

这篇关于从android sqlcipher移动到android sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆