-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
52 lines (45 loc) · 1.39 KB
/
encryption.py
File metadata and controls
52 lines (45 loc) · 1.39 KB
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
import os
import json
import string
import random
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad,unpad
from motor.motor_asyncio import AsyncIOMotorClient
class Encryption:
def __init__(self):
self._ = None
@classmethod
def load_credentials(cls):
if not os.path.isfile("./resources/credentials.json"):
raise FileNotFoundError("Encryption Credentials file not found.")
credentials: dict = json.load(open("./resources/credentials.json", "r"))
cls.key = credentials.get("key")
cls.iv = credentials.get("iv")
@classmethod
def generate_credentials(cls):
cls.key = ''.join(
random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)
for _ in range(16)
)
cls.iv = ''.join(
random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits)
for _ in range(16)
)
json.dump(
{
"key": cls.key,
"iv": cls.iv
},
open("./resources/credentials.json", "w"),
ensure_ascii = False,
indent = 4
)
def encrypt(self, data):
data = pad(data.encode(),16)
cipher = AES.new(self.key.encode('utf-8'),AES.MODE_CBC, self.iv)
return base64.b64encode(cipher.encrypt(data))
def decrypt(self, encrypted_data):
encrypted_data = base64.b64decode(encrypted_data)
cipher = AES.new(self.key.encode('utf-8'), AES.MODE_CBC, self.iv)
return unpad(cipher.decrypt(encrypted_data), 16)