加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
aes.py 1.08 KB
一键复制 编辑 原始数据 按行查看 历史
Vittoz 提交于 2021-05-18 14:00 . Travis 支持 (#507)
from Crypto.Cipher import AES
import hashlib
import base64
import os
import sys
class AEScoder():
def __init__(self,key):
md = hashlib.md5();
md.update(key.encode('utf-8')) #制定需要加密的字符串
realkey = md.hexdigest();
self.__key = realkey.encode("utf-8");
# AES加密
def encrypt(self,data):
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
cipher = AES.new(self.__key, AES.MODE_ECB)
encrData = cipher.encrypt(pad(data))
encrData = base64.b64encode(encrData)
return encrData.decode('utf-8')
# AES解密
def decrypt(self,encrData):
encrData = base64.b64decode(encrData)
# unpad = lambda s: s[0:-s[len(s)-1]]
unpad = lambda s: s[0:-s[-1]]
cipher = AES.new(self.__key, AES.MODE_ECB)
decrData = unpad(cipher.decrypt(encrData))
return decrData.decode('utf-8')
if __name__ == "__main__":
t = AEScoder("testkey");
e = t.encrypt("123");
print (e);
p = t.decrypt(e);
print ("\n",p);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化