# -*- coding: utf-8 -*-
__author__ = 'florije'

import base64

from Crypto.Cipher import AES
from pkcs7 import PKCS7Encoder

# 使用256位的AES，Python会根据传入的Key长度自动选择，长度为16时使用128位的AES
key = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
mode = AES.MODE_CBC
iv = '1234567812345678'  # AES的CBC模式使用IV

encoder = PKCS7Encoder()
text = "This is for test."

def encrypt(data):
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    padded_text = encoder.encode(data)
    encrypted_data = encryptor.encrypt(padded_text)

    return base64.b64encode(encrypted_data)

def decrypt(data):
    cipher = base64.b64decode(data)
    decryptor = AES.new(key, AES.MODE_CBC, iv)
    plain = decryptor.decrypt(cipher)

    return encoder.decode(plain)

encrypted_text = encrypt(text)
clean_text = decrypt(encrypted_text)

print "encrypted_text:", encrypted_text
print "clean_text: ", clean_text