Android: how to check if a database is already encrypted?

I have an Android application which works with a database need to check for being encrypted, and encrypt it if not. I don’t want to save the encrypted database to a new file. I know I can try opening it with an empty password and catch “Net.sqlcipher.database.SQLiteException: file is encrypted or is not a database” but I don’t like catching exceptions, not the least because they are slow. So I would like to ask if there’s some API in the Android version which can tell me if the database is already encrypted.
Thank you for your time.

Dimitar Dobrev,

Hi @ddobrev

You could attempt to read the first 16 bytes of the database file, a plain text SQLite database will have the value SQLite format 3\000, SQLCipher will store per database salt in this location so the value will be different. With regard to encrypting a plain text database, you will need to use sqlcipher_export(…) convenience function, however it does require operating on a separate file.

Hello @developernotes

Thank you for your reply. The encryption itself is OK, I had already got it. My question only concerns checking for encryption. I had seen the approach with the first 16 bytes in an external source and I wasn’t very happy with it, therefore my question here. String comparisons are in most cases a hack, for example, the format will probably change to 4 when they release SQLite 4 and then the check won’t work. But I guess that since there seems to be no better way, I’ll have to make my peace with it.

Dimitar Dobrev,