1

我想生成一个密钥,然后用它用 SQLCipher 加密 Room DB,但问题是 SQLCipher 需要一个 CharArray 作为密钥来加密 SQLite 数据。有没有办法使用 secretKey 而不是 CharArray 或至少将 secretKey 转换为 CharArray?

我生成密钥的代码:

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
    }
4

0 回答 0