-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.py
54 lines (36 loc) · 853 Bytes
/
encryption.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
from cryptography.fernet import Fernet
""""
#create key
key = Fernet.generate_key()
file = open('key.key', 'wb')
file.write(key)
file.close()
"""
"""
#Decrypt the encrypted message
f2 = Fernet(key)
decrypted = f2.decrypt(encrypted)
"""
def encrypt(text, key_file):
#Get the key from the file
file = open(str(key_file), 'rb')
key=file.read()
file.close()
##Encode the text
encoded = text.encode()
#Encrypt the message
f = Fernet(key)
encrypted = f.encrypt(encoded)
#Decode the message
value = encrypted.decode()
return value
def decrypt(text, key):
#Encode the text
encoded=text.encode()
#get the key from the key file
file = open(str(key), 'rb')
key = file.read()
file.close()
f2 = Fernet(key)
decrypted=f2.decrypt(encoded)
return decrypted.decode()