-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenc_dec.py
More file actions
42 lines (39 loc) · 1.22 KB
/
enc_dec.py
File metadata and controls
42 lines (39 loc) · 1.22 KB
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
import os
from cryptography.fernet import Fernet
from repetitive import sav
from repetitive import add_file
def encryption(filepath):
try:
open(filepath, "r")
except FileNotFoundError:
return
key = Fernet.generate_key()
key_path = sav("Choose Key File Path (Please save as '.key' file)")
try:
with open(key_path, "wb") as key_file:
key_file.write(key)
except FileNotFoundError:
return
with open(filepath,"rb") as file:
content = file.read()
content_encrypted = Fernet(key).encrypt(content)
save_file_path = sav("Select Encrypted File Path")
with open(save_file_path, "wb") as file:
file.write(content_encrypted)
def decryption(filepath):
try:
open(filepath, "r")
except FileNotFoundError:
return
key_path = add_file("Select Key File Path")
try:
with open(key_path, "rb") as key_file:
key = key_file.read()
except:
return
with open(filepath,"rb") as file:
content = file.read()
content_decrypted = Fernet(key).decrypt(content)
save_file_path = sav("Select Decrypted File Path")
with open(save_file_path, "wb") as file:
file.write(content_decrypted)