def aes128_decrypt(self, msg):
iv = os.urandom(16)
aes_obj = AES.new(self.key, AES.MODE_CBC, iv)
decrypted_msg = aes_obj.decrypt(msg)
return decrypted_msg
I am using this to decrypt and msg is being passed in as a bytearray. I am using Python 3 and the pycryptodome library for AES128 encryption. The error I am seeing is:
msg = bytearray(b'M\xb1\xbfw\xf4o\x15\xff\xda{u\xba)\xcd\x9fu\x80\xb2\x0c*s\x17%6\xfeA\xb84\xab\x89\xff\x16A\xb8')
def expect_byte_string(data):
if not byte_string(data) and not isinstance(data, Array):
raise TypeError("Only byte strings can be passed to C code")
TypeError: Only byte strings can be passed to C code
The message to decrypt must be a bytes
object, not a bytearray
.
In your second snipped, try to define msg
directly as:
msg = b'M\xb1\xbfw\xf4o\x15\xff\xda{u\xba)\xcd\x9fu\x80\xb2\x0c*s\x17%6\xfeA\xb84\xab\x89\xff\x16A\xb8'
TypeError: Only byte strings can be passed to C code
use A prefix of 'b' or 'B'
or
bytes(s, encoding = "utf8") # str to bytes
Example:
# coding: utf-8
from Crypto.Cipher import AES
import base64
pad_it = lambda s: bytes(s+(16 - len(s)%16)*PADDING, encoding='utf8')
key = b'1234567812345678'
iv = b'1234567812345678'
source = 'Test String'
generator = AES.new(key, AES.MODE_CBC, iv)
crypt = generator.encrypt(pad_it(source))
cryptedStr = base64.b64encode(crypt)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.