Created
December 9, 2011 16:37
-
-
Save jckdotim/1452269 to your computer and use it in GitHub Desktop.
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 | |
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