"""
@作 者: Edison
@日 期: 2020/9/3
@文件名: cryptogram.py
@功能简介: 加密解密模块,支持中文
"""
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
KEY = 'JMT-ABC-FG7-FRI0'
class Crypto:
AES_LENGTH = 16
def __init__(self, key: str = KEY) -> None:
self.key = str(key).zfill(16)[:16].encode()
self.mode = AES.MODE_ECB
self.cryptic = AES.new(self.key, self.mode)
def text_processor(self, text: str) -> str:
# 支持中文 -> text.encode('utf-8')
return text + " " * (self.AES_LENGTH - len(text.encode('utf-8')) % self.AES_LENGTH)
def encrypt(self, text: str) -> bytes:
# 加密
return b2a_hex(self.cryptic.encrypt(self.text_processor(text).encode()))
def decrypt(self, bytecode: bytes) -> str:
# 解密
return self.cryptic.decrypt(a2b_hex(bytecode)).decode().rstrip()
python简单加密解密
最新推荐文章于 2024-05-08 10:31:15 发布