AES python 字符串加密解密

#-*- coding:utf-8 -*-

from Crypto.Cipher import AES
import base64


class AesEncry(object):
    key = r'****************'  # aes秘钥 自己随便设置

    def encrypt(self, data):
        mode = AES.MODE_ECB
        padding = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode("utf-8")
        cryptos = AES.new(self.key, mode)
        cipher_text = cryptos.encrypt(padding(data.encode("utf-8")))
        return base64.b64encode(cipher_text).decode("utf-8")

    def decrypt(self, data):
        try:
            cryptos = AES.new(self.key, AES.MODE_ECB)
            decrpytBytes = base64.b64decode(data)
            meg = cryptos.decrypt(decrpytBytes).decode('utf-8')
            return meg[:-ord(meg[-1])]
        except:
            return ''

if __name__ == '__main__':
#加密解密 aes
= AesEncry() print(aes.encrypt("provinceName=北京市&industryOne=房地产行业")) #OxmyJVVrZ4jfd1/kse+xrCGuOZ8qgp5NlW7lMUDt9uO/dvboYDgUXOe1ZBVLlGNGfBSwo9yE5sOmKxDICai8IQ== print(aes.decrypt("OxmyJVVrZ4jfd1/kse+xrCGuOZ8qgp5NlW7lMUDt9uO/dvboYDgUXOe1ZBVLlGNGfBSwo9yE5sOmKxDICai8IQ=="))

 

posted @ 2022-06-29 17:01  cup_leo  阅读(307)  评论(0编辑  收藏  举报