Help, I need to encrypt my Android database!

A pentest, or vulnerability scan, or customer (!!) told me that my Android database, or other sensitive information, is unencrypted!

We recognize that encrypting on top of the (probably) encrypted disk and OS sandbox is an extra, sometimes unnecessary, layer (see our Threat Model for this). But for one reason or another, you find yourself needing to do this.

Surely you’re not the only one who needs to encrypt something on top of the Android OS, right? You just want to ship this feature, and protect your customers’ data. Why is this hard?

Enter Danny Rosseau, Android Superhero! Danny put together the exact answer you need:

private const val ENC_KEYSTORE_PROVIDER = "AndroidKeyStore"
private const val ENC_APP_DATA_KEY_ALIAS = "application_data"
...
val keyGen = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ENC_KEYSTORE_PROVIDER)
keyGen.init(
   KeyGenParameterSpec.Builder(ENC_APP_DATA_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT.or(KeyProperties.PURPOSE_DECRYPT))
       .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
       .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
       .build()
)
keyGen.generateKey()

This code snippet generates a strong encryption key, and has Android handle storing it securely. Read all the details, including how to encrypt and decrypt using this key, in Danny’s technical post (scroll down to “Android’s KeyStore API” if you just want the answers).

Article Link: https://carvesystems.com/news/help-i-need-to-encrypt-my-android-database/