2

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;

    }
6
  • You can no longer use your SQLCipher version? Commented Jun 24, 2014 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. Commented Jun 24, 2014 at 12:10
  • Please check this question stackoverflow.com/q/20965861/2156937. Commented Jun 24, 2014 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" Commented Jun 24, 2014 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. Commented Jun 24, 2014 at 13:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.