-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (32 loc) · 902 Bytes
/
main.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
from cryptography.fernet import Fernet
# Generate Key
def gen_key():
key = Fernet.generate_key()
with open('key.txt', 'wb') as f:
f.write(key)
# Open File With Key
def get_key():
with open('key.txt', 'rb') as f:
key = f.read() # get key and save as key
return key
# Encrypt Message
def encrypt(key):
message = input("Message: ").encode() # get message
f = Fernet(key)
encrypted = f.encrypt(message)
with open('encrypted.txt', 'wb') as f:
f.write(encrypted)
# Get File With Message
def get_msg():
with open('encrypted.txt', 'rb') as f:
encrypted = f.read() # get encrypted message
return encrypted
# Decrypt Message
def decrypt(key, encrypted):
f = Fernet(key)
decrypted = f.decrypt(encrypted).decode()
return decrypted
# gen_key()
# encrypt(get_key())
message = decrypt(get_key(), get_msg())
print(message)