#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-12-04 10:43:14
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from Crypto.Cipher import AES
# from tornado.options import options
import hashlib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import base64

# iv=options.token_key
# key=options.token_key

#加密
def encrypt(data,key):
    lenth = len(data)
    num = lenth % 16
    data = data.ljust(lenth + 16 - num)
    obj = AES.new(key, AES.MODE_CBC, key)
    result = obj.encrypt(data)
    return base64.urlsafe_b64encode(result)
    
     
#解密
def decrypt(encrypted,key):
    encrypted = base64.urlsafe_b64decode(encrypted)
    obj = AES.new(key, AES.MODE_CBC, key)
    return obj.decrypt(encrypted)

####新的前端加密方式
def aes_base64_encrypt( data, key):
    cipher = AES.new(key)
    return base64.b64encode(cipher.encrypt(_pkcs7padding(data)))

def _pkcs7padding(data):

    size = AES.block_size
    count = size - len(data) % size
    if count:
        data += (chr(count) * count)
    return data

encrypt_str = aes_base64_encrypt('13161086620', 'Q9NY!MMh2VoQjXgx')
print encrypt_str
print "md5_encrypt:"+(hashlib.md5(encrypt_str.encode('utf-8')).hexdigest())
# print decrypt_mode_cbc(encrypt,'www.phperz.com!!')

8d79ebd5c4c6aec11e6c9af19e20fd5988e609dc
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-12-04 10:43:14
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from Crypto.Cipher import AES
# from tornado.options import options
import hashlib
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import base64

# iv=options.token_key
# key=options.token_key

#加密
def encrypt(data,key):
    lenth = len(data)
    num = lenth % 16
    data = data.ljust(lenth + 16 - num)
    obj = AES.new(key, AES.MODE_CBC, key)
    result = obj.encrypt(data)
    return base64.urlsafe_b64encode(result)
    
     
#解密
def decrypt(encrypted,key):
    encrypted = base64.urlsafe_b64decode(encrypted)
    obj = AES.new(key, AES.MODE_CBC, key)
    return obj.decrypt(encrypted)

####新的前端加密方式
def aes_base64_encrypt( data, key):
    cipher = AES.new(key)
    return base64.b64encode(cipher.encrypt(_pkcs7padding(data)))

def _pkcs7padding(data):

    size = AES.block_size
    count = size - len(data) % size
    if count:
        data += (chr(count) * count)
    return data

encrypt_str = aes_base64_encrypt('13161086620', 'Q9NY!MMh2VoQjXgx')
print encrypt_str
print "md5_encrypt:"+(hashlib.md5(encrypt_str.encode('utf-8')).hexdigest())
# print decrypt_mode_cbc(encrypt,'www.phperz.com!!')
