forked from guhy233/XdagTG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.py
47 lines (34 loc) · 1.48 KB
/
crypt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
import base64
import hashlib
def aes_encrypt(key, text):
key = key.encode('utf-8')
if len(key) > 32:
key = key[:32]
elif len(key) < 32:
key = key.ljust(32, b'\0')
iv = b'1145141919810000'
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(text.encode('utf-8')) + padder.finalize()
encryptor = cipher.encryptor()
encrypted_text = encryptor.update(padded_data) + encryptor.finalize()
return base64.b64encode(encrypted_text).decode('utf-8')
def aes_decrypt(key, cdtext):
key = key.encode('utf-8')
if len(key) > 32:
key = key[:32]
elif len(key) < 32:
key = key.ljust(32, b'\0')
iv = b'1145141919810000'
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encrypted_text = base64.b64decode(cdtext)
decryptor = cipher.decryptor()
padded_text = decryptor.update(encrypted_text) + decryptor.finalize()
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
text = unpadder.update(padded_text) + unpadder.finalize()
return text.decode('utf-8')
def sha256(text):
return hashlib.sha256(text.encode()).hexdigest()