Passing a byte array passphrase to SQLCipher via Java

We have an SQLCipher DB that we access from C++. The DB’s passphrase is a byte array which we pass to sqlite3_key() and manage to open the DB successfully.

We need to access this DB from the Java side. Since the SQLCipher Java interface does not provide a method that accepts a byte array, we have to use the method that accepts a char array (SQLiteDatabase.openDatabase()). So, we have to convert our byte array to a char array. We couldn’t find a way to convert our passphrase (stored as a byte array) to a Java char array that can open our DB successfully (a char is 16 bits in Java).

One thing to note is that when converting the byte array to char array from the C++ side, not all characters are readable. So, we can’t use this string to open the DB via an external tool like “DB Browser for SQLite”.

1 Like

The current SQLCipher for Android supports byte[] passwords — see this and this, for example. I’m not certain if your Java use case is for Android, though.

@commonsguy Thanks so much! It worked.

We were using SQLCipher 4.1.3 where these methods were not yet available.

Hi @petrakeas,

Support for accessing database via byte[] was added in SQLCipher for Android 4.1.0, here is the commit.

Hello @petrakeas - I also wanted to mention that we just published some additional guidance on using random values as SQLCipher keys.