TypeError:参数1必须是字符串或只读缓冲区,而不是列表

2024-10-02 22:23:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个AES-ECB 128位函数:

def EncodeAES_ECB(strMessage,tabKey):     
    strMessage = strMessage.encode('utf8')
    bs = AES.block_size
    cipher = AES.new(tabKey, AES.MODE_ECB)
    resData = cipher.encrypt(strMessage)
    return resData

我应该通过打印来测试我的功能:

print (EncodeAES_ECB("Elements",[161, 216, 149, 60, 177, 180, 108, 234, 176, 12, 149, 45, 255, 157, 80, 136])==b'Z\xf5T\xef\x9f\x8bg\x15\xb3E\xe7&gm\x96\x1d')

我有一个错误:TypeError:参数1必须是字符串或只读缓冲区,而不是list 通常,列表的每个元素是1个字节,因此键可以是16个字节。 如何将list:[161, 216, 149, 60, 177, 180, 108, 234, 176, 12, 149, 45, 255, 157, 80, 136]转换为字符串或只读缓冲区中函数EncodeAES_CBC内的[161, 216, 149, 60, 177, 180, 108, 234, 176, 12, 149, 45, 255, 157, 80, 136],以便list的每个数字都可以是一个字节

非常感谢


Tags: 函数字符串字节defutf8listencodeaes