Created
December 12, 2013 11:45
-
-
Save errord/7926810 to your computer and use it in GitHub Desktop.
python crypto AES
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
# -*- 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