Skip to content

Instantly share code, notes, and snippets.

@errord
Created December 12, 2013 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save errord/7926810 to your computer and use it in GitHub Desktop.
Save errord/7926810 to your computer and use it in GitHub Desktop.
python crypto AES
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import os
BS = AES.block_size
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-ord(s[-1])]
key = os.urandom(16) # the length can be (16, 24, 32)
text = 'to be encrypted'
cipher = AES.new(key)
encrypted = cipher.encrypt(pad(text)).encode('hex')
print encrypted # will be something like 'f456a6b0e54e35f2711a9fa078a76d16'
decrypted = unpad(cipher.decrypt(encrypted.decode('hex')))
print decrypted # will be 'to be encrypted'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment