0

我正在尝试在 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.txtflag{flagflag}。加密的输出是0KSF5koIceXxszsgzpl4uA==

binascii.a2b_base64(b'0KSF5koIceXxszsgzpl4uA==')openssl enc -aes-128-ecb -nosalt -pass pass:abcde -md sha256 -in test.txt -out out.txt产生与(no )相同的字节数组-base64,所以我知道 base64 解码应该是第一步。

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

key与以下输出匹配,所以我知道密钥生成代码是正确的:

$ 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)
4

1 回答 1

1

我发现了问题:密钥编码错误:它不是编码key为 utf8 来获取字节数组,它应该被key视为十六进制数字并将其解码为字节数组。

cipher = AES.new(bytearray.fromhex(key), AES.MODE_ECB)
msg = cipher.decrypt(m)
print(msg)
于 2020-02-29T11:34:41.537 回答