How to use Android KeyStore secretKey to encreypt SQLite db with SQLCipher?

990 views Asked by At

I want to generate a key and then use it to encrypt Room DB with SQLCipher, but the problem is SQLCipher requires a CharArray as a key to encrypt SQLite data. is there a way to use secretKey instead of CharArray or at least convert the secretKey to CharArray?.

My code to generate the key :

private val keyGenerator: KeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")

    private val keyGenParameterSpec = KeyGenParameterSpec.Builder(
        KEY_ALIAS,
    KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
        .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
        .build()

        keyGenerator.init(keyGenParameterSpec)
        keyGenerator.generateKey()

    fun getKey(): SecretKey {
        val keyStore = KeyStore.getInstance("AndroidKeyStore")
        keyStore.load(null)

        val secretKeyEntry = keyStore.getEntry(KEY_ALIAS, null) as KeyStore.SecretKeyEntry
        return secretKeyEntry.secretKey
    }
0

There are 0 answers