使用Pycryptodome库for python时,我遇到了一个类型错误:只有字节字符串才能传递给C代码;每当我试图解密

使用Pycryptodome库for python时,我遇到了一个类型错误:只有字节字符串才能传递给C代码;每当我试图解密,python,python-3.x,pycrypto,pycryptodome,Python,Python 3.x,Pycrypto,Pycryptodome,我用它来解密,并且msg作为bytearray传入。我正在使用Python3和pycryptodome库进行AES128加密。我看到的错误是: 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 要解密的消息

我用它来解密,并且msg作为bytearray传入。我正在使用Python3和pycryptodome库进行AES128加密。我看到的错误是:

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

要解密的消息必须是
字节
对象,而不是
字节数组

在第二次截取中,尝试将
msg
直接定义为:

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

TypeError:只能将字节字符串传递给C代码

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'

例如:

bytes(s, encoding = "utf8")  # str to bytes
bytes(s, encoding = "utf8")  # str to bytes
# 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)