Skip to content

Instantly share code, notes, and snippets.

@sachadee
Last active June 20, 2024 08:05
Show Gist options
  • Save sachadee/dc06cc0b7747578e578a470ade0a5f2d to your computer and use it in GitHub Desktop.
Save sachadee/dc06cc0b7747578e578a470ade0a5f2d to your computer and use it in GitHub Desktop.
Python AES ECB Encryption
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad
#AES ECB mode without IV
data = 'I love Medium'
key = 'AAAAAAAAAAAAAAAA' #Must Be 16 char for AES128
def encrypt(raw):
raw = pad(raw.encode(),16)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
return base64.b64encode(cipher.encrypt(raw))
def decrypt(enc):
enc = base64.b64decode(enc)
cipher = AES.new(key.encode('utf-8'), AES.MODE_ECB)
return unpad(cipher.decrypt(enc),16)
encrypted = encrypt(data)
print('encrypted ECB Base64:',encrypted.decode("utf-8", "ignore"))
decrypted = decrypt(encrypted)
print('data: ',decrypted.decode("utf-8", "ignore"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment