-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
33 lines (29 loc) · 1.04 KB
/
modules.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
#Import the cryptography library
from cryptography.fernet import Fernet
from os import path
class modulesForSymmetricCryptography():
def encrypt(self, filename, key):
f = Fernet(key)
file = open(filename, "rb")
if file.mode == 'rb':
content = file.read()
encrypted_data = f.encrypt(content)
newFile = open(filename, "wb")
newFile.write(encrypted_data)
def decrypt(self, filename, key):
f = Fernet(key)
file = open(filename, "rb")
if file.mode == 'rb':
encrypted_data = file.read()
decrypted_data = f.decrypt(encrypted_data)
newFile = open(filename, "wb")
newFile.write(decrypted_data)
def load_key(self):
return open("symmetricKey.key", "rb").read()
def genrate_key(self):
key = Fernet.generate_key()
if(not path.exists("symmetricKey.key")):
symmetric_key = open("symmetricKey.key", "wb")
symmetric_key.write(key)
return 1
return 0