Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple AES enc/dec python
import struct
from Crypto.Cipher import AES
def pad16(s):
t = struct.pack('>I', len(s)) + s.encode('utf-8')
return t + b'\x00' * ((16 - len(t) % 16) % 16)
def unpad16(s):
n = struct.unpack('>I', s[:4])[0]
return s[4:n + 4]
class Crypt(object):
def __init__(self, password):
password = pad16(password)
self.cipher = AES.new(password, AES.MODE_ECB)
def encrypt(self, s):
s = pad16(s)
return self.cipher.encrypt(s)
def decrypt(self, s):
t = self.cipher.decrypt(s)
return unpad16(t)
def encrypt(s, p):
c = Crypt(p)
return c.encrypt(s)
def decrypt(s, p):
c = Crypt(p)
return c.decrypt(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.