Android 密钥库解密加密数据给出了错误的结果

Android Keystore decrypting encrypted data gives incorrect result

我正在尝试使用 java.security.KeyPairGenerator 的实例生成的密钥对来加密任意字符串。不幸的是,在使用生成的密钥对对字符串进行加密和解密后,结果不正确。

以下是我的做法:

val ks: KeyStore = KeyStore.getInstance("AndroidKeyStore").apply {
    load(null)
}

fun encryptUsingKey(publicKey: PublicKey, bytes: ByteArray): ByteArray {
    val inCipher = Cipher.getInstance("RSA/NONE/NoPadding")
    inCipher.init(Cipher.ENCRYPT_MODE, publicKey)
    return inCipher.doFinal(bytes)
}

fun decryptUsingKey(privateKey: PrivateKey, bytes: ByteArray): ByteArray {
    val inCipher = Cipher.getInstance("RSA/NONE/NoPadding")
    inCipher.init(Cipher.DECRYPT_MODE, privateKey)
    return inCipher.doFinal(bytes)
}

fun getKey(): KeyStore.Entry {
    val containsAlias = ks.containsAlias(alias)
    if (!containsAlias) {
        val kpg: KeyPairGenerator = KeyPairGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_RSA,
            "AndroidKeyStore"
        )
        val parameterSpec: KeyGenParameterSpec =
            KeyGenParameterSpec.Builder(
                alias,
                KeyProperties.PURPOSE_DECRYPT or KeyProperties.PURPOSE_ENCRYPT
            )
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                .setRandomizedEncryptionRequired(false)
                .build()

        kpg.initialize(parameterSpec)

        val kp = kpg.generateKeyPair()
    }
    return ks.getEntry(alias, null)
}

我的 encryption/decryption 测试是这样的:

fun testEncryptionDecryption() {
    val entry = getKey()

    if (entry is KeyStore.PrivateKeyEntry) {
        val privateKey = entry.privateKey
        val certificate = entry.certificate
        val publicKey = certificate.publicKey

        val testKey = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"

        val encrypted = service.encryptUsingKey(publicKey, Base64.decodeFromString(testKey))
        val decrypted = service.decryptUsingKey(privateKey, encrypted)

        assertEquals(testKey, Base64.encodeToString(decrypted))

    }
}

不幸的是结果是这样的:

org.junit.ComparisonFailure: expected:<[0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF]> but was:<[AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANNdt-Oeu_PQAQgxBdNdt-Oeu_PQAQgxBdNdt-Oeu_PQAQgxBdNdt-Oeu_PQAQgxBQ]>

谁能告诉我这是怎么回事?这些 A 是从哪里来的?我是不是按键用错了?

怀疑配置不正确。以下作品:

val inCipher = Cipher.getInstance("RSA/ECB/OAEPPadding")

val kpg: KeyPairGenerator = KeyPairGenerator.getInstance(
            KeyProperties.KEY_ALGORITHM_RSA,
            "AndroidKeyStore"
        )
        val parameterSpec: KeyGenParameterSpec =
            KeyGenParameterSpec.Builder(
                alias,
                KeyProperties.PURPOSE_DECRYPT or KeyProperties.PURPOSE_ENCRYPT
            )
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
                .setBlockModes(KeyProperties.BLOCK_MODE_ECB)
                .setDigests(KeyProperties.DIGEST_SHA1)
                .build()

        kpg.initialize(parameterSpec)