使用 AES 加密进行探测.无法使用正确的密钥解密



我正在尝试解密加密数据。数据使用pycryptodome lib使用AES CBC模式加密。有这样的错误 - "值错误:不正确的 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"" * (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"")
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)

相关内容

  • 没有找到相关文章

最新更新