Python 在PyCrypto中重新创建openssl解密命令

Python 在PyCrypto中重新创建openssl解密命令,python,openssl,aes,pycryptodome,Python,Openssl,Aes,Pycryptodome,我正在尝试用PyCrypto重新创建第二个命令解密 openssl enc -aes-128-ecb -nosalt -base64 -pass pass:abcde -md sha256 -in test.txt -out out.txt openssl enc -d -aes-128-ecb -nosalt -base64 -pass pass:abcde -md sha256 -in out.txt test.txt的内容是flag{flagsflag}。加密输出为0KSF5KOICEX

我正在尝试用PyCrypto重新创建第二个命令解密

openssl enc -aes-128-ecb -nosalt -base64 -pass pass:abcde -md sha256 -in test.txt -out out.txt
openssl enc -d -aes-128-ecb -nosalt -base64 -pass pass:abcde -md sha256 -in out.txt
test.txt的内容是flag{flagsflag}。加密输出为0KSF5KOICEXSZSGZPL4UA==

binascii.a2b_base64b'0KSF5koIceXxszsgzpl4uA=='生成与openssl enc-aes-128-ecb-nosalt-pass-pass相同的字节数组:abcde-md sha256-in test.txt-out.txt no-base64,因此我知道base64解码应该是第一步

p = "abcde".encode()
h = SHA256.new(p)
key = h.hexdigest()[:32].upper()
密钥与以下输出匹配,因此我知道密钥生成代码是正确的:

$ openssl enc -aes-128-ecb -nosalt -pass pass:abcde -base64 -md sha256 -in test.txt -v -P
key=36BBE50ED96841D10443BCB670D6554F
bufsize=8192
然而,把这些放在一起会产生msg中的垃圾。如果能指出我错在哪里,我们将不胜感激

from Crypto.Hash import SHA256
from Crypto.Cipher import AES

import binascii

c = binascii.a2b_base64(b'0KSF5koIceXxszsgzpl4uA==')

p = "abcde".encode()
h = SHA256.new(p)
key = h.hexdigest()[:32].upper()

cipher = AES.new(key.encode(), AES.MODE_ECB)
msg = cipher.decrypt(m)
print(msg)

我发现问题:密钥编码是错误的:它不是将密钥编码为utf8以获取bytearray,它应该将密钥视为十六进制数并将其解码为bytearray

cipher = AES.new(bytearray.fromhex(key), AES.MODE_ECB)
msg = cipher.decrypt(m)
print(msg)