"""
@作 者: 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:
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
()
转载请注明原文地址:https://tech.qufami.com/read-7625.html