1 Star 0 Fork 1

云诊科技(舌象特征AI)/ai-tongue-python

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
aes.py 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
云诊科技(舌象特征AI) 提交于 2023-09-08 11:08 +08:00 . update aes.py.
# -*- coding: utf-8 -*-
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
BLOCK_SIZE = 16
KEY = "base64 encoded aes key"
class AesFunc(object):
def __init__(self):
key_b64decode = base64.b64decode(KEY)
# ECB模式
self.cipher = AES.new(key_b64decode, AES.MODE_ECB)
def encrypt(self, plaintext):
plaintext_bytes = plaintext.encode('utf-8')
# pkcs7填充
plaintext_paded = pad(plaintext_bytes, BLOCK_SIZE)
# 加密
ciphertext_bytes = self.cipher.encrypt(plaintext_paded)
# base64编码
ciphertext = base64.b64encode(ciphertext_bytes)
return ciphertext.decode('utf-8')
def decrypt(self, ciphertext):
# base64解码
ciphertext_b64decode = base64.b64decode(ciphertext)
# 解密
plaintext_bytes = self.cipher.decrypt(ciphertext_b64decode)
# 取消填充
plaintext = unpad(plaintext_bytes, BLOCK_SIZE)
return plaintext.decode('utf-8')
def run(self, msg):
ciphertext = self.encrypt(msg)
print("密文: " + ciphertext)
plaintext = self.decrypt(ciphertext)
print("明文: " + plaintext)
if __name__ == '__main__':
msg = '测试 AES/ECB/PKCS5Padding 加解密'
AesFunc().run(msg)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/ai-tongue/ai-tongue-python.git
git@gitee.com:ai-tongue/ai-tongue-python.git
ai-tongue
ai-tongue-python
ai-tongue-python
master

搜索帮助