• 对称加密 Crypto


    1, python  对称加密使用,

    import ast
    from base64 import b64encode
    from Crypto.Util.Padding import pad
    from base64 import b64decode
    from Crypto.Cipher import AES
    from Crypto.Util.Padding import unpad
    
    # key只能传入bytes
    key = 'ssssssssssfsssssssssssssssfsssss'.encode() 
    
    def pad_key(key):  # 限制只能传入长度为16,32
        while len(key) % 16 != 0:
            key += b' '     #不足的用空格补全
        return key
    
    def get_sign(data):
        cipher = AES.new(key, AES.MODE_CBC)
        ct_bytes = cipher.encrypt(pad(data, AES.block_size))
        iv = b64encode(cipher.iv).decode('utf-8')
        ct = b64encode(ct_bytes).decode('utf-8')
        print(iv)
        print(ct)
        return iv,ct    # 返回偏移和加密数据
    
    
    def de_sign(iv, sign):
        iv = b64decode(iv)
        ct = b64decode(sign)
        cipher = AES.new(key, AES.MODE_CBC, iv)
        pt = unpad(cipher.decrypt(ct), AES.block_size)
        return ast.literal_eval(str(pt, encoding='utf-8'))
    
    
    if __name__ == '__main__':
        data = str({'name':'xxx','um':"xxx"}).encode()
        iv,sign = get_sign(data)
        data = de_sign(iv,sign)
        print(data)

      适用于对数据加传输

  • 相关阅读:
    mysql8 JDBC连接注意事项
    Flask上下文管理源码分析
    python基础学习day5
    python基础学习day4
    python基础学习day3
    python基础学习day2
    python基础学习day1
    简单的名片管理系统
    pycharm教程
    Python:知识目录
  • 原文地址:https://www.cnblogs.com/wjun0/p/15514170.html
Copyright © 2020-2023  润新知