-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
50 lines (41 loc) · 1.23 KB
/
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
44
45
46
47
48
from cryptography.fernet import Fernet
from keygen import getKey
import tkinter as tk
from tkinter import filedialog
def encrypt(key, file):
with open(file,"rb") as f:
message = f.read()
f.close()
fernet = Fernet(getKey(key))
cipherTxt = fernet.encrypt(message)
with open(file+".encrypted" ,"wb") as f:
f.write(cipherTxt)
f.close()
def decrypt(key,file):
with open(file,"rb") as f:
message = f.read()
f.close()
fernet = Fernet(getKey(key))
# Check if its the right key
try:
base = os.path.splitext(file)[0]
plainTxt = fernet.decrypt(message)
with open(base+".decrypted","wb") as f:
f.write(plainTxt)
f.close()
except:
print("Invalid key")
root = tk.Tk()
method = int(input("1 - Encription\n2 - Decryption\nChose method: "))
key = input("Key: ")
if method == 1:
files = filedialog.askopenfilenames()
for file in files:
if file.endswith(".encrypted"):
print(file, " is Already encrypted")
else:
encrypt(key, file)
elif method == 2:
files = filedialog.askopenfilenames(filetypes=(("Encrypted","*.encrypted"),))
for file in files:
decrypt(key, file)