aes加解密

# -*- coding=utf-8-*-
from Crypto.Cipher import AES
import os
from Crypto import Random
import base64

"""
aes加密算法
padding : PKCS7
"""

class AESUtil:

    __BLOCK_SIZE_16 = BLOCK_SIZE_16 = AES.block_size

    @staticmethod
    def encryt(str, key, iv):
        cipher = AES.new(key, AES.MODE_CBC,iv)
        x = AESUtil.__BLOCK_SIZE_16 - (len(str) % AESUtil.__BLOCK_SIZE_16)
        if x != 0:
            str = str.decode('utf-8') + chr(x)*x
            str=str.encode('utf-8')
        msg = cipher.encrypt(str)
        # msg = base64.urlsafe_b64encode(msg).replace('=', '')
        msg = base64.b64encode(msg)
        return msg

    @staticmethod
    def decrypt(enStr, key, iv):
        cipher = AES.new(key, AES.MODE_CBC, iv)
        # enStr += (len(enStr) % 4)*"="
        # decryptByts = base64.urlsafe_b64decode(enStr)
        decryptByts = base64.b64decode(enStr)
        msg = cipher.decrypt(decryptByts)
        print(msg)
        paddingLen = msg[len(msg)-1]
        return msg[0:-paddingLen]

if __name__ == "__main__":
    key = "123456798".encode("utf-8")#你的秘钥
    iv = "123456789".encode("utf-8")#你的密匙

     # 2eDiseYiSX62qk/WS/ZDmg==
    # mobile ='SneyylhQ9iZ/8kgRhX1Ppw=='.encode("utf-8")
    mobile ='15843056370'.encode("utf-8")
    res = AESUtil.encryt(mobile, key, iv)

    print(res)
    # print (AESUtil.decrypt(mobile, key, iv)) # 123456

 https://www.cnblogs.com/xuchunlin/p/11421795.html
  
  

下载解密Crypto模块

https://www.cnblogs.com/fawaikuangtu123/p/9761943.html

原文地址:https://www.cnblogs.com/zhangqing979797/p/11563329.html