Skip to content

Instantly share code, notes, and snippets.

@matandobr
Last active February 16, 2021 07:53
Show Gist options
  • Save matandobr/ff90f210da81cbdb2ae3fc70a566f64b to your computer and use it in GitHub Desktop.
Save matandobr/ff90f210da81cbdb2ae3fc70a566f64b to your computer and use it in GitHub Desktop.
pycryptodome python 3.6.8 AES decryption
# Tested on Windows 10 with Python 3.6.8
# pycryptodome==3.10.1
# Tested with AES 128 but should work with others
# Based on https://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256 thread
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
IV_ASCII = 'ffaaffaaffaaffaaffaaffaaffaaffaa' # example of 16 bytes len of ascii-represented hex string IV
AES_KEY = '\xaa' * 16 # example of 16 bytes len binary key
with open('file.decrypted', 'wb') as ofd:
with open('file.encrypted', 'rb') as ifd:
iv = bytes.fromhex(IV_ASCII)
cipher = AES.new(AES_KEY, AES.MODE_CBC, iv=iv) # Setup cipher
original_data = unpad(cipher.decrypt(ifd.read()), AES.block_size) # Decrypt and then up-pad the result
ofd.write(original_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment