-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.py
86 lines (76 loc) · 2.5 KB
/
program.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
78
79
80
81
82
83
84
85
86
from cryptography.fernet import Fernet
import uuid
fernet = ""
def decryptfile():
location = input("Please provide the file location to decrypt: ")
try:
with open(location, 'rb') as enc_file:
encrypted = enc_file.read()
decrypted = fernet.decrypt(encrypted)
with open(location, 'wb') as dec_file:
dec_file.write(decrypted)
print("File decrypted successfully")
print("=============================================")
except:
print("Failed to decrypt the file. Please verify the file location or the privatekey provided.")
menu()
def encryptfile():
location = input("Please provide the file location to encrypt: ")
try:
with open(location, 'rb') as file:
original = file.read()
encrypted = fernet.encrypt(original)
with open(location, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
print("File encrypted successfully");
print("=============================================")
except:
print("Failed to detect or encrypt the file provided -> " + location)
menu()
def menu():
print("0 - Exit");
print("1 - Encrypt File")
print("2 - Decrypt File")
user_option = input("Your option: ")
if(user_option == "0"):
return
switcher = {
"1": encryptfile,
"2": decryptfile
}
try:
switcher.get(user_option, "Invalid option")()
except:
print("Invalid option provided")
def readprivatekey():
location = input("Please provide your key location: ")
global fernet
try:
with open(location, 'rb') as filekey:
key = filekey.read()
fernet = Fernet(key)
print("Private key read successfully.");
menu()
except:
print("failed to read the private key provided.")
print("========================================")
initialize()
def initialize():
print("Please provide your option:\n")
print("1 - Generate key")
print("2 - Encrypt/Decrypt File")
user_option_input = input("Your option: ")
switcher = {
"1" : generatenewkey,
"2" : readprivatekey
}
try:
switcher.get(user_option_input,"invalid option provided")()
except:
print("invalid option provided")
def generatenewkey():
key = Fernet.generate_key()
file_name = str(uuid.uuid4())
with open(file_name +'_private.key', 'wb') as filekey:
filekey.write(key)
initialize()