Skip to content

Instantly share code, notes, and snippets.

@luk0y
Created May 29, 2021 05:16
Show Gist options
  • Save luk0y/24271eee5cb7bc702a59f6304eeb2a2f to your computer and use it in GitHub Desktop.
Save luk0y/24271eee5cb7bc702a59f6304eeb2a2f to your computer and use it in GitHub Desktop.
AES ECB 128 bit encryption in python3. 100% working
from Crypto import Random
from Crypto.Cipher import AES
import base64
from pkcs7 import PKCS7Encoder
# References
# AES 128 Bit ECB mode https://www.devglan.com/online-tools/aes-encryption-decryption
# https://stackoverflow.com/a/64545969
def encrypt_data(text_data):
#could just be like 'abcdefghwhatever'
encryption_key = "YOUR SECRET KEY HERE" #You can retreive from external file if you want
#convert to bytes. same as bytes(encryption_key, 'utf-8')
encryption_key = str.encode(encryption_key);
#padding
encoder = PKCS7Encoder();
raw = encoder.encode(text_data) # Padding
iv = Random.new().read(AES.block_size ) #AES.block_size defaults to 16
cipher = AES.new( encryption_key, AES.MODE_ECB)
encrypted_text = base64.b64encode(cipher.encrypt( str.encode(raw) ) )
return encrypted_text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment