Error error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT when I try to decripted

644 Views Asked by At

In this test, I send a wrapped value and get the encrypted key. Then I try to decrypted it using AES/GCM/NOPADDING and a valid transportKeyIV. Finally in the cipher.doFinal() method I got this error message:

javax.crypto.AEADBadTagException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at com.android.org.conscrypt.OpenSSLAeadCipher.throwAEADBadTagExceptionIfAvailable(OpenSSLAeadCipher.java:320)
at com.android.org.conscrypt.OpenSSLAeadCipher.doFinalInternal(OpenSSLAeadCipher.java:371)
at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:374)
at javax.crypto.Cipher.doFinal(Cipher.java:2056)
at com.geopagos.core.security.keystore.KeystoreWrapTest.decodeWrappedCertificate(KeystoreWrapTest.kt:274)
at com.geopagos.core.security.keystore.KeystoreWrapTest.access$decodeWrappedCertificate(KeystoreWrapTest.kt:36)
at com.geopagos.core.security.keystore.KeystoreWrapTest$testUnwrapCertManually$1.invokeSuspend(KeystoreWrapTest.kt:215)
at com.geopagos.core.security.keystore.KeystoreWrapTest$testUnwrapCertManually$1.invoke(Unknown Source:8)
at com.geopagos.core.security.keystore.KeystoreWrapTest$testUnwrapCertManually$1.invoke(Unknown Source:4)

This is my code:

private  fun decode(
    certificateStr : String,
    expectedTransportKey : String,
    expectedSecureKey : String
) {
    val derValue = DERGeneralString.fromByteArray(certificateStr.hexToByteArray()) as DLSequence

    val encryptedSecureKey = (derValue.elementAt(4) as DEROctetString).octets

    // Decrypting
    val secretKey = SecretKeySpec(plainTransportKey, KeyProperties.KEY_ALGORITHM_AES)
    val cipher = Cipher.getInstance("${KeyProperties.KEY_ALGORITHM_AES}/${KeyProperties.BLOCK_MODE_GCM}/${KeyProperties.ENCRYPTION_PADDING_NONE}")
    val parameterSpec: AlgorithmParameterSpec = GCMParameterSpec(128, transportKeyIV)
    cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec)

    // HERE is where it crashed
    val result = cipher.doFinal(encryptedSecureKey)

    expectThat(plainSecureKey.toHexString()).isEqualTo(result)
}

What could be the reason?

0

There are 0 best solutions below