zoukankan      html  css  js  c++  java
  • python进阶(23)用Python实现AES_ECB_PKCS5加密

    前言

    AES加密的模式有很多种,下面来介绍ECB模式的加密解密

    import base64
    from Crypto.Cipher import AES
    
    
    class AESECB:
        def __init__(self, key):
            self.key = key  # 加密密钥
            self.mode = AES.MODE_ECB  # 设置为ECB模式
            self.bs = 16  # block size
            self.PADDING = lambda s: s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
    
        def encrypt(self, text):
            generator = AES.new(self.key, self.mode)  # ECB模式无需向量iv
            crypt = generator.encrypt(self.PADDING(text).encode('utf-8'))
            crypted_str = base64.b64encode(crypt).decode('utf-8')
            return crypted_str
    
        def decrypt(self, text):
            generator = AES.new(self.key, self.mode)  # ECB模式无需向量iv
            decrpyt_bytes = base64.b64decode(text)
            meg = generator.decrypt(decrpyt_bytes).decode('utf-8')
            return meg[:-ord(meg[-1])]
    
    
    if __name__ == '__main__':
        aes = AESECB('1234567890abcdef')
        print(aes.encrypt('111111'))
        print(aes.decrypt('rfTzn9WjsDFbK262m0k4xg=='))
    

    结果:

    rfTzn9WjsDFbK262m0k4xg==
    111111
    
  • 相关阅读:
    网页Tab控件
    ivy在eclipse中的重新加载
    es删除文档或者删除索引
    es修改数据
    es中插入数据
    创建es索引-格式化和非格式化
    MySQL常用字符串函数
    python各种类型转换
    python 3.4读取输入参数
    python异常捕获异常堆栈输出
  • 原文地址:https://www.cnblogs.com/jiakecong/p/15683471.html
Copyright © 2011-2022 走看看