• python AES解密


    AES解密

    安装

    pip install Crypto
    

    copy可用

    import base64
    from Crypto.Cipher import AES
    
    
    class EncryptDate:
        def __init__(self, key):
            self.key = key.encode('utf-8')  # 初始化密钥
            self.length = AES.block_size  # 初始化数据块大小
            self.aes = AES.new(self.key, AES.MODE_ECB)  # 初始化AES,ECB模式的实例
            # 截断函数,去除填充的字符
            self.unpad = lambda date: date[0:-ord(date[-1])]
    
        def pad(self, text):
            """
            #填充函数,使被加密数据的字节码长度是block_size的整数倍
            """
            count = len(text.encode('utf-8'))
            add = self.length - (count % self.length)
            entext = text + (chr(add) * add)
            return entext
    
        def encrypt(self, encrData):  # 加密函数
            res = self.aes.encrypt(self.pad(encrData).encode("utf8"))
            msg = str(base64.b64encode(res), encoding="utf8")
            return msg
    
        def decrypt(self, decrData):  # 解密函数
            res = base64.decodebytes(decrData.encode("utf8"))
            print(res)
            msg = self.aes.decrypt(res).decode("utf8")
            print(msg)
            return self.unpad(msg)
    
    
    text = 'Password0313!'  # 待加密文本
    key = '123'  # 密钥
    
    eg = EncryptDate(key)  # 这里密钥的长度必须是16的倍数
    
    res = eg.encrypt(text)  # 加密函数
    print(res)
    
    res = eg.decrypt(res)  # 解密函数
    print(res)
    
  • 相关阅读:
    【信号 10】kill函数、raise函数
    【信号 10】信号
    git remote
    分片上传 multipart
    【线程同步】屏障
    【进程间通信】信号量
    【信号 | 10】alarm函数、setitimer 函数
    【rpm】rpm设置
    va_start和va_end使用详解
    idea为什么提示:Duplicated code fragment (**lines long)
  • 原文地址:https://www.cnblogs.com/qiukangle/p/15907633.html
Copyright © 2020-2023  润新知