Created
October 12, 2023 21:22
-
-
Save ustayready/57a9e9f6805694f3da82e7c88137b3e0 to your computer and use it in GitHub Desktop.
Simple AES ECB Encryption/Decryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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