Python关于AES加密程序的运行出现error,有大神相助吗?

代码如下:(AES CBC模式加密)

# 导入图像处理标准库
from PIL import Image
# 导入Cryptodome库,引用aes加密模块
from Cryptodome import AES
import random
import string


# 随机生成16个由小写字母组成的字符串
def key_generator(size = 16, chars = string.ascii_lowercase):
    return ''.join(random.choice(chars) for _ in range(size))


#filename = "d:/blackhat.bmp"
#filename_encrypted_cbc = "blackhat_encrypted_cbc"
#formatt = "BMP"
# 利用函数随机生成一个由小写字母组成的字符串
#key = key_generator(16)
#print("key:"+key)


# AES加密的明文空间为16的整数倍,不能整除,则需要进行填充
# 在对应的ascii中,"\x00"表示为0x00,具体的值为NULL,b表示以字节表示
def pad(data):
    return data + b"\x00" * (16 - len(data) % 16)



# 将图像的数据映射为RGB
def trans_format_rbg(data):
    # tuple:不可变,保证数据不丢失
    red, green, blue = tuple(map(lambda e: [data[i] for i in range(0, len(data)) if i % 3 == e], [0, 1, 2]))
    pixels = tuple(zip(red, green, blue))
    return pixels


# CBC加密
def aes_cbc_encrypt(key1, data, mode=AES.MODE_CBC):
    # iv为随机值
    iv = key_generator(16)
    aes = AES.new(key1, mode, iv)
    new_data = aes.encrypt(data)
    return new_data


def encrypt_image_cbc(filename1):
# 打开bmp图片,然后将之转换为RGB图像
        im = Image.open(filename1)
        value_vector = im.convert("RGB").tobytes()

        # 将图像数据转换为像素值字节
        imlength = len(value_vector)

        # 将填充、加密后的数据进行像素值映射
        value_encrypt = trans_format_rbg(aes_cbc_encrypt(key, pad(value_vector))[:imlength])

        # 创建一个新对象,存储相对应的值
        im2 = Image.new(im.mode, im.size)
        im2.putdata(value_encrypt)

        # 将对象保存为对应格式的图像
        im2.save(filename_encrypted_cbc + "." + formatt, format)
        # im2.save(filename_encrypted_cbc+".bmp")


key = key_generator(16)
filename = "d:/blackhat.bmp"
filename_encrypted_cbc = "blackhat_encrypted_cbc"
formatt = "BMP"
encrypt_image_cbc(filename)
im = Image.open(filename)
im.show()

运行后出现如下错误:
Traceback (most recent call last):
File "C:/Users/GYW/PycharmProjects/untitled/CBC encrypt", line 74, in
encrypt_image_cbc(filename)
File "C:/Users/GYW/PycharmProjects/untitled/CBC encrypt", line 59, in encrypt_image_cbc
value_encrypt = trans_format_rbg(aes_cbc_encrypt(key, pad(value_vector))[:imlength])
File "C:/Users/GYW/PycharmProjects/untitled/CBC encrypt", line 45, in aes_cbc_encrypt
aes = AES.new(key1, mode, iv)
File "C:\Users\GYW\PycharmProjects\untitled\venv\lib\site-packages\Cryptodome\AES.py", line 232, in new
return create_cipher(sys.modules[_name__], key, mode, *args, **kwargs)
File "C:\Users\GYW\PycharmProjects\untitled\venv\lib\site-packages\Cryptodome\Cipher__init__.py", line 79, in _create_cipher
return modesmode
File "C:\Users\GYW\PycharmProjects\untitled\venv\lib\site-packages\Cryptodome\Cipher_mode_cbc.py", line 274, in _create_cbc_cipher
cipher_state = factory._create_base_cipher(kwargs)
File "C:\Users\GYW\PycharmProjects\untitled\venv\lib\site-packages\Cryptodome\AES.py", line 103, in _create_base_cipher
result = start_operation(c_uint8_ptr(key),
File "C:\Users\GYW\PycharmProjects\untitled\venv\lib\site-packages\Cryptodome\Util_raw_api.py", line 238, in c_uint8_ptr
raise TypeError("Object type %s cannot be passed to C code" % type(data))
TypeError: Object type cannot be passed to C code

Process finished with exit code 1

刚接触python 求大神拉一把~

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问