代码如下:
from binascii import b2a_hex, a2b_hex
from urllib import unquote
from Crypto.Cipher import AES
from Padding import appendSpacePadding
class AesHandler(object):
def __init__(self, **kwargs):
"""
key,id是30环境A0008的
"""
self.key = kwargs.get("key", "!I50#LSSciCx&q6E")
self.iv = kwargs.get('iv', "$t%s%12#2b474pXF")
self.mode = AES.MODE_CBC
self.BS = AES.block_size
def encrypt(self, text):
text = appendSpacePadding(text)
cipher = AES.new(self.key, self.mode, self.iv)
cipher_text = cipher.encrypt(text)
return b2a_hex(cipher_text)
def decrypt(self, text):
cipher = AES.new(self.key, self.mode, self.iv)
plain_text = cipher.decrypt(a2b_hex(text))
return unquote(plain_text)