Sqlcipher encrypt export with byte array password

This is database encryption example in github
(sqlcipher-android-tests/ImportUnencryptedDatabaseTest.java at master · sqlcipher/sqlcipher-android-tests · GitHub)

database = SQLiteDatabase.openOrCreateDatabase(unencryptedDatabase, “”, null);
database.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’”,
encryptedDatabase.getAbsolutePath(), ZeteticApplication.DATABASE_PASSWORD));
database.rawExecSQL(“select sqlcipher_export(‘encrypted’)”);
database.rawExecSQL(“DETACH DATABASE encrypted”);
database.close();

after that, I cannot open with same password.
Because there’s difference between byte array password and converted string.
rawExecSQL using only string format.

If I open database like below, it opened. (byte password)

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(sourceDbFile.getAbsolutePath(), “”, null);
destDbFile.getAbsolutePath().length()-3);
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”, destDbFile.getAbsolutePath(), password));
db.rawExecSQL(“SELECT sqlcipher_export(‘encrypted’)”);
db.rawExecSQL(“DETACH DATABASE encrypted;”);
db.close();

db.SQLiteDatabase.openOrCreateDatabase(destDbFile, String.valueOf(password), null)

Is there another way to passing byte array parameter?

Hi @eternalblue

You can use the ? within the string for binding parameter values. Would you give that a try?

Thanks for reply.
I just want to bind parameter by byte array value like below.

(byte password)
db.rawExecSQL(String.format(“ATTACH DATABASE ‘%s’ AS encrypted KEY ‘%s’;”, destDbFile.getAbsolutePath(), password));

getWritableDatabse, getReadableDatabase API can get byte array value,
but there’s no way to passing byte array when sqlcipher_export query.

Hi @eternalblue

You can use execSQL(...) to pass an Object[] for your parameters. You can do that in conjunction with using the ? operator within your SQL statement itself.

Hi @eternalblue

I just wanted to call attention to some of our guidance around keying a database, please make sure you are using a valid UTF-8 sequence of key material when opting for a byte array.