将sqlite转换为加密数据库:

4

我在我的应用程序中有一个现有的sqlite数据库。它已经成功运行。现在我需要使用sqlcipher加密数据库。我的问题是在将sqlite转换为加密时出现以下异常。

net.sqlcipher.database.SQLiteException: table android_metadata already exists
 at net.sqlcipher.database.SQLiteDatabase.native_rawExecSQL(Native Method)
 at net.sqlcipher.database.SQLiteDatabase.rawExecSQL(SQLiteDatabase.java:1851)
 at com.x.y.convert_sqlite_to_sqlcipher(Practitioner_menu.java:2626)
 at com.x.y$AdminProcess.doInBackground(Practitioner_menu.java:1659)
 at com.x.y$AdminProcess.doInBackground(Practitioner_menu.java:1)
 at android.os.AsyncTask$2.call(AsyncTask.java:287)
 at java.util.concurrent.FutureTask.run(FutureTask.java:234)
 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
 at java.lang.Thread.run(Thread.java:856)

我的转换代码:

File old_sqliteFile = getDatabasePath("old_db.sqlite");
File databaseFile = getDatabasePath("new_db.db");
        databaseFile.mkdirs();
        databaseFile.delete();
    database = SQLiteDatabase.openOrCreateDatabase(databaseFile,
                    "password", null);
database.rawExecSQL(String.format(
                    "ATTACH DATABASE '%s' AS encrypted KEY '%s'",
                    databaseFile.getAbsolutePath(), "password");
            database.rawExecSQL("select sqlcipher_export('encrypted')");
            database.rawExecSQL("DETACH DATABASE encrypted");
            database.close();

请帮我找出我做错了什么?


@maven 我觉得我们的问题几乎一样,但错误不同。你是如何解决这个问题的? - Amsheer
2个回答

7

您在两次使用databaseFile。应该针对old_sqliteFile调用openOrCreateDatabase(),而不是databaseFile

以下是一种方法,可以用加密的替代品替换未加密的数据库文件:

  public static void encrypt(Context ctxt, String dbName,
                             String passphrase) throws IOException {
    File originalFile=ctxt.getDatabasePath(dbName);

    if (originalFile.exists()) {
      File newFile=
          File.createTempFile("sqlcipherutils", "tmp",
                              ctxt.getCacheDir());
      SQLiteDatabase db=
          SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
                                      "", null,
                                      SQLiteDatabase.OPEN_READWRITE);

      db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
                                  newFile.getAbsolutePath(), passphrase));
      db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
      db.rawExecSQL("DETACH DATABASE encrypted;");

      int version=db.getVersion();

      db.close();

      db=
          SQLiteDatabase.openDatabase(newFile.getAbsolutePath(),
                                      passphrase, null,
                                      SQLiteDatabase.OPEN_READWRITE);
      db.setVersion(version);
      db.close();

      originalFile.delete();
      newFile.renameTo(originalFile);
    }
  }

我在这个问题上遇到了困难。无论如何,以下代码失败:db.rawExecSQL("SELECT sqlcipher_export('encrypted')"); 我无法继续进行任何操作,因为数据库已经损坏。如何处理它?如果再次运行这组代码仍然没有成功怎么办?根据你的回答,我认为这个链接https://dev59.com/aH3aa4cB1Zd3GeqPXAHq是已经损坏了。 - Amsheer
那么你会如何建议进行encrypt()调用呢?我会收到错误吗,还是只需要在DB的onUpgrade调用中执行此操作? - user2676468
@user2676468:你不能在SQLiteOpenHelperonUpgrade()调用中这样做,因为数据库已经打开了。虽然没有db.isEncrypted()选项,但如果您可以使用常规SQLite(或使用空密码短语的SQLCipher)打开数据库,则知道该数据库未加密。当您拥有未加密的数据库并且用户指示她想要切换到加密的数据库时,您就知道何时调用encrypt() - CommonsWare
作为参数的空口令短语是指 "" 吗? - user2676468
用户表示她想切换到加密数据库。如果我不想给用户选择,只想为每个人进行此操作,最好的解决方案是什么? - user2676468
显示剩余11条评论

0

谢谢,它很好用。 我添加了这段代码来加密、解密和更改密码。

@Override
public synchronized SQLiteDatabase getWritableDatabase(String password) {
    SQLiteDatabase sqLiteDatabase;
    try{
        sqLiteDatabase = super.getWritableDatabase(password);
    }catch(Exception e){
        try {
            encrypt(context,DATABASE_NAME,PWOLD,PWNEW);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        sqLiteDatabase = super.getWritableDatabase(password);
    }
    return sqLiteDatabase;
}

@Override
public synchronized SQLiteDatabase getReadableDatabase(String password) {
    SQLiteDatabase sqLiteDatabase;
    try{
        sqLiteDatabase = super.getReadableDatabase(password);
    }catch(Exception e){
        try {
            encrypt(context, DATABASE_NAME, PWOLD, PWNEW);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        sqLiteDatabase = super.getReadableDatabase(password);
    }
    return sqLiteDatabase;
}

public static void encrypt(Context context, String dbName,
                           String oldPass, String newPass) throws IOException {
    if(!newPass.isEmpty()) Log.d(TAG,"Encrypt DB...");
    else Log.d(TAG,"Decrypt DB...");

    File originalFile=context.getDatabasePath(dbName);

    if (originalFile.exists()) {
        File newFile = File.createTempFile("sqlcipherutils", "tmp",
                        context.getCacheDir());
        SQLiteDatabase db = SQLiteDatabase.openDatabase(originalFile.getAbsolutePath(),
                oldPass, null,
                SQLiteDatabase.OPEN_READWRITE);

        db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s';",
                newFile.getAbsolutePath(), newPass));
        db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
        db.rawExecSQL("DETACH DATABASE encrypted;");

        int version=db.getVersion();

        db.close();

        db = SQLiteDatabase.openDatabase(newFile.getAbsolutePath(),
                newPass, null,
                SQLiteDatabase.OPEN_READWRITE);
        db.setVersion(version);
        db.close();

        originalFile.delete();
        newFile.renameTo(originalFile);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接