AES 加密的问题。不能用正确的密钥解密

Probles with AES encrypting. cant decrypt with a right key

我正在尝试解密加密数据。数据使用 AES CBC 模式使用 pycryptodome lib 加密。 有这样的错误 - “ValueError:不正确的 AES 密钥长度(256 字节)

import os
from Crypto import Random
from Crypto.Cipher import AES

class AESCipher:
    def __init__(self, key):
        pass

    def pad(self, s):
        return s + b"[=10=]" * (AES.block_size - len(s) % AES.block_size)

    def encrypt(self, message, key, key_size=256):
        message = self.pad(message)
        iv = Random.new().read(AES.block_size)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        return iv + cipher.encrypt(message)

    def decrypt(self, ciphertext, key):
        iv = ciphertext[:AES.block_size]
        cipher = AES.new(key, AES.MODE_CBC, iv)
        plaintext = cipher.decrypt(ciphertext[AES.block_size:])
        return plaintext.rstrip(b"[=10=]")

def send_data(data)
    key = os.urandom(16)
    cipher = AESCipher(key)
    ciphertext = cipher.encrypt(data, key)
    return key, ciphertext

def receive_data(key, data):
    cipher = AESCipher(key)
    decrypted = cipher.decrypt(data, key)
    return decrypted

data = b'12 43 42 46 af'
key, ciphertext = send_data(data)
decrypted = receive_data(key, data)

我觉得你要解密的是密文,不是原文data(未加密):

decrypted = receive_data(key, ciphertext)