Search code examples
pythonkotlinencryptionaes

Python to Kotlin AES communication (javax.crypto.BadPaddingException)


I am trying to encrypt a UTF-8 string on Python with AES and decrypt it in Kotlin, here is the Python encrypt part:

class aes():

    def __init__(self, key):
        self.bs = AES.block_size
        self.key = hashlib.sha256(key.encode()).digest()

    def encrypt(self, raw):
        raw = self.pad(raw)
        iv = self.key[:self.bs]  # Random.new().read(self.bs)
        cipher = AES.new(self.key, AES.MODE_CBC, iv)
        return base64.b64encode(iv + cipher.encrypt(raw.encode()))

    def pad(self, s):
        return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

...

client_key = self.rsa.decrypt(int(connection.recv(bufsize).decode()))

enc = aes(client_key)
em = enc.encrypt("AES_OK")

connection.send(em+b'\0')

And the Kotlin part decrypting it:

object AES256 {
    private val decorder = Base64.getDecoder()
    private fun cipher(opmode:Int, secretKey:String):Cipher {

        val c = Cipher.getInstance("AES/CBC/PKCS7Padding")

        val sk = SecretKeySpec(secretKey.toByteArray(Charsets.UTF_8), "AES")
        val iv = IvParameterSpec(secretKey.substring(0, 16).toByteArray(Charsets.UTF_8))
        c.init(opmode, sk, iv)
        return c
    }
    @RequiresApi(Build.VERSION_CODES.O)
    fun decrypt(str:String, secretKey:String):String {
        val byteStr = decorder.decode(str.toByteArray(Charsets.UTF_8))
        // Error here
        return String(cipher(Cipher.DECRYPT_MODE, secretKey).doFinal(byteStr))
    }
}

fun readBytes(input: BufferedReader) : String {
    var byte: Int = input.read()
    var r = ""
    while(byte != 0) {
        r += byte.toChar()
        byte = input.read()
    }
    return r
}

resposta = readBytes(input)
resposta = AES256.decrypt(resposta, atc_aesk.toString(Charsets.UTF_8))

And I get the following exception:

javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
  1. All the RSA and connection code works properly, the ciphertext and key are the same on both sides.
  2. I'm using a 32 bytes key, I also tried a 16 bytes one, same error.

I would greatly appreciate any help or input, thanks.


Solution

  • The user President James K. Polk answered this in a comment, the mistake was that I hashed the key only in the Python code and not in Kotlin... self.key = hashlib.sha256(key.encode()).digest()

    I tried finding the problem like a whole week, I really feel so dumb, thank you President James K. Polk.