#!/bin/env python
#-*- coding:utf8 -*-

from Crypto.Cipher import AES
import base64


def aes_encode(text):
    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 = 'gua*nyuhuaxia*gkaifasunweis*heng'
    iv = 'sun1we9is8he1ngw'
    mode = AES.MODE_CBC

    cipher = AES.new(key, mode, iv)
    encrypted = base64.encodestring(cipher.encrypt(pad(text)))
    return encrypted


def aes_decode(text):
    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 = 'gua*nyuhuaxia*gkaifasunweis*heng'
    iv = 'sun1we9is8he1ngw'
    mode = AES.MODE_CBC

    str = base64.decodestring(text)
    cipher = AES.new(key, mode, iv)
    plain = unpad(cipher.decrypt(str))
    return plain

if __name__ == '__main__':
    text = '6NFcmF8EX2s/REoGe+1tAg=='
    print aes_decode(text)
10-14 07:26