Cipher:字符串加解密
1from Crypto.Cipher import AES
2from binascii import b2a_hex, a2b_hex
3
4"""
5pip install pycryptodome
6"""
7
8
9class Cipher(object):
10    MODE = AES.MODE_CBC
11    DEFAULT_KEY = b"024ea589b1f070da"
12
13def__init__(self, key: bytes = b"024ea589b1f070da"):
14"""key: 16位, 24位, 32位"""
15        self.DEFAULT_KEY = key
16
17    @classmethod
18def encrypt(cls, text: str):
19"""这⾥密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.⽬前AES-128⾜够⽤"""
20        cipher = w(cls.DEFAULT_KEY, cls.MODE, cls.DEFAULT_KEY)
21        length = 16
22        count = len(text)
23        add = length - (count % length)
24        text += '\0' * add
密码字符串是什么
25return b2a_de())).decode()
26
27    @classmethod
28def decrypt(cls, text: str):
29        cipher = w(cls.DEFAULT_KEY, cls.MODE, cls.DEFAULT_KEY)
30        plain_text = cipher.decrypt(a2b_hex(text))
31return plain_text.rstrip(b'\0').decode()
32
33
34if__name__ == '__main__':
35    cipher = Cipher()
36    text = pt('hello')
37print(text)
38print(cipher.decrypt('f9cca2bb60e577874271c538c6c06161'))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。