0

I am trying to recreate the second command (decryption) in 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

Content of test.txt is flag{flagflag}. Output of encryption is 0KSF5koIceXxszsgzpl4uA==.

binascii.a2b_base64(b'0KSF5koIceXxszsgzpl4uA==') produces the same bytearray as openssl enc -aes-128-ecb -nosalt -pass pass:abcde -md sha256 -in test.txt -out out.txt (no -base64), so I know base64 decoding should be the first step.

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

key matches with the following output, so I know the key generation code is correct:

$ openssl enc -aes-128-ecb -nosalt -pass pass:abcde -base64 -md sha256 -in test.txt -v -P
key=36BBE50ED96841D10443BCB670D6554F
bufsize=8192

However, putting this all together produces garbage in msg. Any pointer in where I got it wrong would be appreciated.

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)
John London
  • 1,250
  • 2
  • 14
  • 32

1 Answers1

1

I found the issue: the key encoding is wrong: It is not encoding key to utf8 to get bytearray, it should be treating key as hex number and decode it to bytearray.

cipher = AES.new(bytearray.fromhex(key), AES.MODE_ECB)
msg = cipher.decrypt(m)
print(msg)
John London
  • 1,250
  • 2
  • 14
  • 32