import base64
import binascii
import re
from Crypto.Cipher import AES
class AESCBC:
def __init__(self):
self.key = 'weg2452g&%$#ghsghf'.encode('utf-8') # 定义key值
self.mode = AES.MODE_CBC
self.bs = 16 # block size
self.PADDING = lambda s: s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs) # 不足16长,自动补足,补足规则:少几个就补几个几
# 加密
def encrypt(self, text):
iv = bytes(str(text), encoding='utf-8')[0:16] # 定义动态IV
generator = AES.new(self.key, self.mode, iv)
crypt = generator.encrypt(self.PADDING(text).encode('utf-8'))
crypted_str = base64.urlsafe_b64encode(crypt) # 输出Base64
# crypted_str = binascii.b2a_hex(crypt) # 输出Hex
result = crypted_str.decode()
return result
# 解密
def decrypt(self, text, plain_text):
iv = bytes(str(plain_text), encoding=