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

**URL:** <https://discuss.zetetic.net/t/android-how-to-check-if-a-database-is-already-encrypted/1481>\
**Category:** SQLCipher FAQ\
**Created:** [June 28, 2016, 12:01pm UTC](https://discuss.zetetic.net/t/android-how-to-check-if-a-database-is-already-encrypted/1481 "2016-06-28T12:01:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddobrev](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@ddobrev](https://discuss.zetetic.net/u/ddobrev)\
**Post date:** [June 28, 2016, 12:01pm UTC](https://discuss.zetetic.net/t/android-how-to-check-if-a-database-is-already-encrypted/1481/1 "2016-06-28T12:01:37Z")

</div>

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,

> **[Dimitar Dobrev / QtSharp](https://gitlab.com/ddobrev/QtSharp)**
>
> Mono/.NET bindings for Qt

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [June 28, 2016, 1:12pm UTC](https://discuss.zetetic.net/t/android-how-to-check-if-a-database-is-already-encrypted/1481/2 "2016-06-28T13:12:34Z")

</div>

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(…)`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#sqlcipher_export) convenience function, however it does require operating on a separate file.

---

<div class="post-metadata">

**Author:** ![ddobrev](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@ddobrev](https://discuss.zetetic.net/u/ddobrev)\
**Post date:** [June 28, 2016, 1:28pm UTC](https://discuss.zetetic.net/t/android-how-to-check-if-a-database-is-already-encrypted/1481/3 "2016-06-28T13:28:05Z")

</div>

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,

> **[Dimitar Dobrev / QtSharp](https://gitlab.com/ddobrev/QtSharp)**
>
> Mono/.NET bindings for Qt
