Net.sqlcipher.database.SQLiteException: file is encrypted or is not a database


#1

public static void encrypt(Context ctxt, File unencryptedDatabase,
String passphrase) {
if (unencryptedDatabase.exists()) {

    String encName = unencryptedDatabase.getName().substring(0, unencryptedDatabase.getName().indexOf(".")) + "_enc.db";
    File newFile = new File(unencryptedDatabase.getParent(), encName);

    SQLiteDatabase db= SQLiteDatabase.openDatabase(unencryptedDatabase.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);

    //SQLiteDatabase encodedDB = SQLiteDatabase.openOrCreateDatabase(newFile, passphrase, null);
    // THE FOLLOWING LINE WITH ATTACH DATABASE CAUSES THE ISSUE IN TITLE FIELD
    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.openOrCreateDatabase(newFile, passphrase, null);

    db.setVersion(version);
    db.close();

    }
}

#2

Hi @ghousk

Do you have a specific question with regard to SQLCipher for Android?


#3

Hi Thanks for the quick response, I am following the ImportUnencryptedDatabaseTest very closely, I do have a plain text db file that is supplied into this function and that opens fine, but the above mentioned line for the encryptedDB fails with the issue “Net.sqlcipher.database.SQLiteException: file is encrypted or is not a database”.
Do I need to create this DB before by trying db= SQLiteDatabase.openOrCreateDatabase(encryptedDatabase, passphrase, null); or somethign to make it work, I have tried the example and it works fine in that.


#4

The passphrase is not empty


#5

If I change the routine to use a temp file then it seems to not coomplain on this line any more, but a different issue comes up later on. It doesn’t migrate the tables from the plain DB to the encrypted one, and on opening the encrypted db later complains of missing tables, that are surely supposed to be in it

public static void encrypt(Context ctxt, File unencryptedDatabase,
String passphrase) {
if (unencryptedDatabase.exists()) {

        File encryptedDatabase = null;
        try {
            encryptedDatabase = File.createTempFile("sqlcipherutils", "tmp", ctxt.getCacheDir());
        } catch (IOException e) {
            e.printStackTrace();
        }


        SQLiteDatabase db= SQLiteDatabase.openDatabase(unencryptedDatabase.getAbsolutePath(), "", null, SQLiteDatabase.OPEN_READWRITE);

        String path = encryptedDatabase.getAbsolutePath();
        db.rawExecSQL(String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'", path, passphrase));
        db.rawExecSQL("SELECT sqlcipher_export('encrypted')");
        db.rawExecSQL("DETACH DATABASE encrypted");

        int version=db.getVersion();

        db.close();

        db= SQLiteDatabase.openOrCreateDatabase(encryptedDatabase, passphrase, null);

        db.setVersion(version);
        db.close();

        unencryptedDatabase.delete();
        encryptedDatabase.renameTo(unencryptedDatabase);

    }
}

#6

Hi @ghousk

No, creating the file before hand that you wish to export to is not required, SQLCipher will create it if necessary. Can you try running your scenario through the SQLCipher command line shell? What is the value of this part of your app?

String.format("ATTACH DATABASE '%s' AS encrypted KEY '%s'", newFile.getAbsolutePath(), passphrase);

Thanks!


#7

When I use the temp file solution the SQL string is

“ATTACH DATABASE ‘/data/data/com.connectib.gsk.sitemap.debug/cache/sqlcipherutils-1713371831tmp’ AS encrypted KEY ‘test’”

For the non-temp file solution it is:
“ATTACH DATABASE ‘/storage/emulated/0/gsk_data/1/mydb_enc.db’ AS encrypted KEY ‘test’”


#8

Hi,
I have just fixed it, the problem was when I used the non-temp file solution. The line of code
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’”, path, passphrase));
would raise an exception if the ‘path’ already exists. Many Thanks for your help :smiley:


#9

Hello @ghousk

I’m glad to hear you have resolved the issue. Take care!