2

I have an encyprted SQLite database file that is being exported from an existing application using System.Data.SQLite. Based on what I can find, System.Data.SQLite uses 128-bit RC4 encryption.

When I try to load this database in my Android application with SQLCipher, I get the following error "file is encrypted or is not a database". I have tried using a database hook which sets the pragma key = rc4, but this does not seem to help. The only program I have been able to find that can open this database file is SQLite2009 Pro Enterprise Manager. It opens it with no issue, allows me to browse, and run queries.

Here is the code I am currently using:

        try
    {
        SQLiteDatabase.loadLibs(activity);

        //this function copies the db file from the project assets to the data/databases folder
        copydatabase();

        File databaseFile = activity.getApplicationContext().getDatabasePath("someDB.db3");

        SQLiteDatabaseHook hook = new SQLiteDatabaseHook(){
            public void preKey(SQLiteDatabase database){
                database.execSQL("pragma cipher = rc4");
            }
            public void postKey(SQLiteDatabase database){
                database.execSQL("pragma cipher = rc4");
            }
        };

        SQLiteDatabase database = SQLiteDatabase.openDatabase(databaseFile.getAbsolutePath(), "mypassword", null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READWRITE, hook);

        Cursor cursor = database.rawQuery("SELECT * FROM SOMETABLE;", null);

        if (cursor.moveToFirst()){
            do{
                String data = cursor.getString(cursor.getColumnIndex("SOME_COLUMN"));
            }while(cursor.moveToNext());
        }
        cursor.close();
    }
    catch(Exception ex)
    {
        Log.e(ex.getMessage().toString(), ex.getStackTrace().toString());
    }

Any ideas on what I am doing wrong?

Patrick
  • 148
  • 2
  • 7

2 Answers2

0

Looks like I got an answer over on the SQLCipher Google Group:

If your SQLite database was encrypted outside of SQLCipher, you will first need to export that database to a plain-text SQLite database. Once you have done that you can use the sqlcipher_export convenience function to create an SQLCipher encrypted version your plain-text database. Please refer to example 1 1 in the documentation for sqlcipher_export for your scenario.

http://sqlcipher.net/sqlcipher-api#sqlcipher_export

Patrick
  • 148
  • 2
  • 7
0

Your database may be of an older SQLCipher version than the one you are trying to open it with.

Try to execute the query PRAGMA cipher_migrate; just after opening and setting the key of the database.

Cyril
  • 206
  • 1
  • 5