1

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;

    }
SKP
  • 410
  • 5
  • 20
  • You can no longer use your SQLCipher version? – busylee Jun 24 '14 at 12:03
  • thanks for your response. Earlier when i created my application, i used sqlcipher v2.x.x Now i wanted to migrate to 3.1.0 but could't do that. So now i want to unencrypt my database so that it would become a plain database. – SKP Jun 24 '14 at 12:10
  • Please check this question http://stackoverflow.com/q/20965861/2156937. – busylee Jun 24 '14 at 12:28
  • thanks for the help. I will check the link if it works. I still want to know the reason why SQLiteDatabase.openOrCreateDatabase(FULL_DB_Path, password, null, hook); fails to open the encrypted the file and shows the error " File is encrypted or id not a database" – SKP Jun 24 '14 at 13:04
  • 1
    Please edit your answer. Post your code you use before updating and answer. May be it helps stackoverflow users to help you. – busylee Jun 24 '14 at 13:55
  • hi. i have editted and posted the code. Please look into it. I hope you get some clue where i am going wrong. Thanks in advance. – SKP Jun 24 '14 at 15:15

1 Answers1

0

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Nick Parker
  • 1,378
  • 1
  • 7
  • 10