Created
August 12, 2016 10:06
-
-
Save JobsDong/a83d87668531414d508c5a7439b821e7 to your computer and use it in GitHub Desktop.
aes python encrypt
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
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