Android SQLCipher RC4 encrypted database file “file is encrypted or is not a database”

975 views
Skip to first unread message

Patrick

unread,
Feb 26, 2014, 12:08:35 PM2/26/14
to sqlc...@googlegroups.com

I just posted this on StackOverflow, because I wasn't approved for this group yet and didn't know how long it would take.  Sorry for the double post:

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?

Nick Parker

unread,
Feb 26, 2014, 12:56:55 PM2/26/14
to sqlc...@googlegroups.com
Hello Patrick,

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.

[1] http://sqlcipher.net/sqlcipher-api#sqlcipher_export

On 2/26/14, 11:08 AM, Patrick wrote:
> I just posted this on StackOverflow, because I wasn't approved for this
> group yet and didn't know how long it would take. Sorry for the double
> post:
>
> 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 <http://osenxpsuite.net/download.htm>. 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?
>
> --
>
> ---
> You received this message because you are subscribed to the Google
> Groups "SQLCipher Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sqlcipher+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

--
Nick Parker

signature.asc

Patrick

unread,
Feb 26, 2014, 2:17:39 PM2/26/14
to sqlc...@googlegroups.com
Nick,

Thanks for your quick reply.  Glad I posted here before I wasted any more time trying to get this to work.  We will be exploring our options later today as to what to do about our current situation.  Thanks again!

Dan Davis

unread,
Aug 21, 2014, 10:36:54 PM8/21/14
to sqlc...@googlegroups.com
I ran into a similar problem tonight.  I built the sqlite3.dll from https://github.com/sqlcipher/sqlcipher, using MnGW and some hard work.  I pointed SQL Expert Pro to the .dll and created a new DB using all defaults values and providing an encryption key.  When I brought the .db into my Android application and attempted to open it via db = SQLiteDatabase.openDatabase(DATABASE_PATH, mySQLCipheyKey, null, SQLiteDatabase.OPEN_READWRITE);, I was confronted with the same error.


I would expect that since I used SQLCipher to create the DB, that SQLCipher would play nice, regardless of platform?  Any reasonable ideas?

Dan Davis

unread,
Aug 22, 2014, 2:25:26 AM8/22/14
to sqlc...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages