forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
51 lines (43 loc) · 1.37 KB
/
script.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
from cryptography.fernet import Fernet
import os
import sys
import argparse
def generate_key():
return Fernet.generate_key()
def save_key(key, file_path):
# save encryption key to a file
with open(file_path,'wb') as f:
f.write(key)
def load_key(file_path):
with open(file_path,'rb') as f:
return f.read()
def encrypt_file(key,file_path):
f=Fernet(key)
with open(file_path,'rb') as f_input:
data = f_input.read()
encrypted_data = f.encrypt(data)
with open(file_path.split(".")[0]+'.encrypted','wb') as f_output:
f_output.write(encrypted_data)
os.remove(file_path)
def decrypt_file(key, file_path):
f=Fernet(key)
with open (file_path,'rb') as f_enc:
data = f_enc.read()
dec_data = f.decrypt(data)
with open(file_path.split(".")[0]+".txt",'wb') as f_dec:
f_dec.write(dec_data)
os.remove(file_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description = "[*]Encrypt youre files and password protect them")
parser.add_argument('file_path', help="Add the name of the file to be encrypted")
parser.add_argument('mode',choices=['encrypt','decrypt'],help="Choose either encrypt or decrypt")
args = parser.parse_args()
if args.mode == "encrypt":
key = generate_key()
save_key(key,"secret.key")
encrypt_file(key,args.file_path)
print("Encrypted")
else:
key = load_key("secret.key")
decrypt_file(key, args.file_path )
print("Decrypted")