Skip to content

Instantly share code, notes, and snippets.

@JobsDong
Created August 12, 2016 10:06
Show Gist options
  • Save JobsDong/a83d87668531414d508c5a7439b821e7 to your computer and use it in GitHub Desktop.
Save JobsDong/a83d87668531414d508c5a7439b821e7 to your computer and use it in GitHub Desktop.
aes python encrypt
import base64
from Crypto.Cipher import AES
PADDING = '\0'
pad_it = lambda s: s+(16 - len(s) % 16) * PADDING
def encrypt(content, iv, key):
cipher = AES.new(key, AES.MODE_CBC, iv)
crypt = cipher.encrypt(pad_it(content))
return base64.b64encode(crypt)
def decrypt(content, iv, key):
enc = base64.b64decode(content)
cipher = AES.new(key, AES.MODE_CBC, iv)
enc = cipher.decrypt(enc)
return enc.rstrip(PADDING)
if __name__ == "__main__":
key = '1234567812345678'
iv = '1234567812345678'
encry = encrypt("this is a test", key, iv)
print "encrypt:" + encry
print decrypt(encry, key, iv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment