1
votes

I'm trying to implement aes-128-cbc encryption in Kotin/Java. Unfortunately encryption-results differ from OpenSSL-output using same arguments.

Arguments:

Data: [[[1532347727425, 6481]]]

Key: f70559cb976123807855fd9081f17760

IV: b7e5daacd367e143d20a0b536507a6d6

Kotlin-Implementation:

const val AES_TRANSFORMATION = "AES/CBC/PKCS5Padding"

val cipher = Cipher.getInstance(AES_TRANSFORMATION)
val keySpec = SecretKeySpec(aesKey.hexToByteArray(), "AES")
val ivSpec = IvParameterSpec(iv.hexToByteArray())
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec)
val result = cipher.doFinal(data.toByteArray())
return String(Base64Helper().encode(result))

OpenSSL-CMD:

echo [[[1532347727425, 6481]]] | openssl aes-128-cbc -d -K f70559cb976123807855fd9081f17760 -iv b7e5daacd367e143d20a0b536507a6d6 -base64

Results:

Kotlin/Android: fHcK3EeXEzSYtIa8PbhXkANwv2sU/qgktUnabzgE9HI=
OpenSSL: 7L1XLM/5ihCNRryiSZ8RMlPSeqsHDXa785dJujS7c/M=

Decrypting results with OpenSSL produces the following error:

bad decrypt 
140621119981208:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:529:

Update: Changed Padding to PKCS5 - same output.

1

1 Answers

0
votes

You should use the AES/CBC/PKCS5Padding cipher