Skip to content

Instantly share code, notes, and snippets.

@jckdotim
Created December 9, 2011 16:37
Show Gist options
  • Save jckdotim/1452269 to your computer and use it in GitHub Desktop.
Save jckdotim/1452269 to your computer and use it in GitHub Desktop.
from Crypto.Cipher import AES
import binascii
aes_key = '1234567890123456' # it must be 128-bit.
def aes_encrypt(data):
cipher = AES.new(aes_key)
expected_length = 16 * ((len(data) / 16) + 1)
padding_length = expected_length - len(data)
data = data + chr(padding_length) * padding_length
return binascii.hexlify(cipher.encrypt(data))
def aes_decrypt(data):
cipher = AES.new(aes_key)
dec = cipher.decrypt(binascii.unhexlify(data))
if '\x00' in dec:
dec = dec[:dec.index('\x00')]
last = dec[len(dec) - 1]
dec = dec[:-ord(last)]
return dec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment