forked from tangyoha/telegram_media_downloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.py
77 lines (60 loc) · 2.11 KB
/
crypto.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Crypto utils"""
import base64
from Crypto.Cipher import AES
class AesBase64(object):
"""for AES encryption"""
def __init__(self, key: str, iv: str):
self.key = key.encode("utf-8")
self.iv = iv.encode("utf-8")
self.mode = AES.MODE_CBC
def encrypt(self, content):
"""
Encrypts the given content using the AES encryption algorithm.
Parameters:
content (str): The content to be encrypted.
Returns:
str: The encrypted content encoded in base64.
"""
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
content_padding = self.pkcs7padding(content)
encrypt_bytes = cipher.encrypt(content_padding.encode("utf-8"))
return base64.b64encode(encrypt_bytes)
def decrypt(self, content):
"""
Decrypts the given content using AES encryption
with Cipher Block Chaining (CBC) mode.
Parameters:
content (str): The content to be decrypted.
Returns:
str: The decrypted text.
"""
cipher = AES.new(self.key, AES.MODE_CBC, self.iv)
content = base64.b64decode(content)
text = cipher.decrypt(content).decode("utf-8")
return self.pkcs7unpadding(text)
def pkcs7unpadding(self, text):
"""
Removes the PKCS#7 padding from the given text.
Parameters:
text (str): The text to remove padding from.
Returns:
str: The text without PKCS#7 padding.
"""
length = len(text)
unpadding = ord(text[length - 1])
return text[0 : length - unpadding]
def pkcs7padding(self, text):
"""
Adds PKCS7 padding to the given text.
Args:
text (str): The text to be padded.
Returns:
str: The padded text.
"""
bs = 16
length = len(text)
bytes_length = len(text.encode("utf-8"))
padding_size = length if (bytes_length == length) else bytes_length
padding = bs - padding_size % bs
padding_text = chr(padding) * padding
return text + padding_text