Created
October 23, 2017 16:13
-
-
Save bharadwaj-raju/59ebebfe0f9585d166f660fd3cc8bb70 to your computer and use it in GitHub Desktop.
Simple AES enc/dec python
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
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