Search code examples
aespycrypto

Crypto.Cipher AES.MODE_EAX encrypt_and_digest error: argument 2 must be bytes, not bytearray


The following code gave me the error "argument 2 must be bytes, not bytearray". I can't find any doc about encrypt_and_digest except some example codes which all just passed one argument to the method. Anyone knows what's the issue here? Thanks.

key = rb(16)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(b'hello')

Solution

  • I traced the error source to the third line in the following code

    partial = self._cache[:]
    partial[self._cache_n:] = b'\x80' + b'\x00' * (bs - self._cache_n - 1)
    pt = strxor(strxor(self._last_ct, partial), self._k2)
    

    in the function digest in the file python3.7/site-packages/Crypto/Hash/CMAC.py. The error is about the second argument partial, which is a bytearray. When I modified the third line to the following, the error went away.

    pt = strxor(strxor(self._last_ct, bytes(partial)), self._k2)