zoukankan      html  css  js  c++  java
  • Python3实现AES加解密

    import base64
    from Crypto.Cipher import AES
    from urllib.parse import unquote
    '''
    采用AES对称加密算法
    '''
    # str不是16的倍数那就补足为16的倍数
    def add_to_16(value):
    while len(value) % 16 != 0:
    value += ''
    return str.encode(value) # 返回bytes
    #加密方法
    def encrypt_oracle():
    # 秘钥
    key = 'abcdefgabcdefg12'
    # 待加密文本
    text = input("输入待加密文本:")
    # 初始化加密器
    aes = AES.new(add_to_16(key), AES.MODE_ECB)
    #先进行aes加密
    encrypt_aes = aes.encrypt(add_to_16(text))
    #用base64转成字符串形式
    encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8') # 执行加密并转码返回bytes
    print("加密值;",encrypted_text)
    #解密方法
    def decrypt_oralce():
    # 秘钥
    key = 'abcdefgabcdefg12'
    # 密文
    text = input("输入密文:")
    # 初始化加密器
    aes = AES.new(add_to_16(key), AES.MODE_ECB)
    #优先逆向解密base64成bytes
    base64_decrypted = base64.decodebytes(text.encode(encoding='utf-8'))
    #执行解密密并转码返回str
    decrypted_text = str(aes.decrypt(base64_decrypted),encoding='utf-8').replace('','')
    print("解密值:",decrypted_text)
    print("URL解码后解密值:",unquote(decrypted_text))




    if __name__ == '__main__':
    # encrypt_oracle()
      try:
      decrypt_oralce()
      print("加解密成功")
      except Exception as error:
      print("输入错误:",error)


  • 相关阅读:
    docker 安装 nexus3 初始密码不再是admin123
    eclipse中Tomcat修改项目名称
    WAMP3.1.3自定义根目录
    git学习笔记
    小米和MAC触摸板手势汇总
    IDEA快捷键汇总
    servelet 实现Post接口访问
    LeetCode:Jump Game II
    LeetCode:Trapping Rain Water
    LeetCode: Container With Most Water
  • 原文地址:https://www.cnblogs.com/jiaown123/p/14311575.html
Copyright © 2011-2022 走看看