python AES加密解密

python aes加密 aes解密
创建于:2019年08月29日 更新于:2019年09月02日

安装依赖库

pip install pycryptodome
from binascii import b2a_base64, a2b_base64
from Crypto.Cipher import AES

class AES_CBC():
    def __init__(self, key, iv):
        # key必须为16位字符串
        self.key = bytes(key, encoding='utf-8')
        self.iv = bytes(iv, encoding='utf-8')

    # 加密。text:需要加密的内容
    def encrypt(self, text):
        cryptor = AES.new(self.key, AES.MODE_CBC, self.iv)
        bs = 16 
        length = len(text)
        bytes_length = len(bytes(text, encoding='utf-8'))
        # tips:utf-8编码时,英文占1个byte,而中文占3个byte
        padding_size = length if (bytes_length == length) else bytes_length
        padding = bs - padding_size % bs
        # tips:chr(padding)看与其它语言的约定,有的会使用'\0'
        padding_text = chr(padding) * padding
        text = text + padding_text

        ciphertext = cryptor.encrypt(bytes(text, encoding='utf-8'))

        return b2a_base64(ciphertext)

    # 解密
    def decrypt(self, content):
         cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
         encrypt_bytes = a2b_base64(content) 
         decrypt_bytes = cipher.decrypt(encrypt_bytes)
         text = str(decrypt_bytes, encoding='utf-8') 
         length = len(text)
         unpadding = ord(text[length-1])
         return text[0:length-unpadding]