0
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 作为字节数组传入。我正在使用 Python 3 和 pycryptodome 库进行 AES128 加密。我看到的错误是:

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
4

2 回答 2

0

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

use A prefix of 'b' or 'B' 

或者

bytes(s, encoding = "utf8")  # str to bytes

例子:</p>

# 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)
于 2017-11-21T05:01:39.897 回答
0

要解密的消息必须是bytes对象,而不是bytearray.

在你的第二个片段中,尝试msg直接定义为:

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'
于 2017-05-20T20:32:10.493 回答