Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting sqlite to encrypted database:

I've an existing sqlite database in my application. It running successfully. Now i need to encrypt the database using sqlcipher. My problem is at the time of converting sqlite to encrypt i got the following exception.

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)

My converting code:

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();

Please anyone help me to find out what i am doing wrong?

like image 566
Amsheer Avatar asked Mar 18 '23 23:03

Amsheer


1 Answers

You are using databaseFile twice. Your openOrCreateDatabase() should be called for old_sqliteFile, not databaseFile.

Here is a method that will replace an unencrypted database file with an encrypted replacement:

  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);
    }
  }
like image 196
CommonsWare Avatar answered Mar 29 '23 06:03

CommonsWare