Skip to content

Instantly share code, notes, and snippets.

@ustayready
Created October 12, 2023 21:22
Show Gist options
  • Save ustayready/57a9e9f6805694f3da82e7c88137b3e0 to your computer and use it in GitHub Desktop.
Save ustayready/57a9e9f6805694f3da82e7c88137b3e0 to your computer and use it in GitHub Desktop.
Simple AES ECB Encryption/Decryption
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from base64 import b64encode, b64decode
def encrypt_text(plaintext, key):
key = bytes(key, 'ascii') + b'\x00' * (16-len(key))
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(pad(plaintext.encode(), AES.block_size))
return ciphertext
def decrypt_text(ciphertext, key):
key = bytes(key, 'ascii') + b'\x00' * (16-len(key))
cipher = AES.new(key, AES.MODE_ECB)
decrypted_data = unpad(cipher.decrypt(ciphertext), AES.block_size)
return decrypted_data.decode()
if __name__ == "__main__":
plaintext = "Hello, this is a secret message!"
key = "Hardcoded"
ciphertext = encrypt_text(plaintext, key)
print("Ciphertext (Base64):", b64encode(ciphertext).decode())
decrypted_text = decrypt_text(ciphertext, key)
print("Decrypted Text:", decrypted_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment