0

我正在尝试在 python2.7 中运行以下代码段(key已设置变量):

import binascii
from Crypto.Cipher import AES

iv = binascii.unhexlify(u'd75b8c8b8c8bc1add8f8fcc1add8f8fc')
AES.new(key, AES.MODE_CFB, iv) 

弹出以下异常:

Error: 
    raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type <type 'unicode'> cannot be passed to C code 

根据binascii 文档

Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a TypeError is raised.

pycryptodome 文档

iv (bytes, bytearray, memoryview) – (Only applicable for MODE_CBC, MODE_CFB, MODE_OFB, and MODE_OPENPGP modes).

The initialization vector to use for encryption or decryption.

For MODE_CBC, MODE_CFB, and MODE_OFB it must be 16 bytes long.

For MODE_OPENPGP mode only, it must be 16 bytes long for encryption and 18 bytes for decryption (in the latter case, it is actually the encrypted IV which was prefixed to the ciphertext).

If not provided, a random byte string is generated (you must then read its value with the iv attribute).

变量的iv长度是偶数,那么为什么会引发 TypeError 呢?

4

0 回答 0