-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenc.py
75 lines (57 loc) · 1.7 KB
/
enc.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
from cryptography.fernet import Fernet
import os
import argparse
def generate_key():
key = Fernet.generate_key()
with open('master.key', 'wb') as key_file:
key_file.write(key)
def get_key():
return open('master.key', 'rb').read()
def decrypt(items, key):
f = Fernet(key)
for item in items:
item_orig = item.rsplit('.', 1)[0]
print(item)
os.rename(item, item_orig)
item = item_orig
with open(item, 'rb') as file:
encrypted_data = file.read()
decrypted_data = f.decrypt(encrypted_data)
with open(item, 'wb') as file:
file.write(decrypted_data)
def encript(items, key, ex):
f = Fernet(key)
for item in items:
with open(items, 'rb') as files:
file_data = files.read()
encrypted_data = f.encrypt(file_data)
with open(item, 'wb') as file:
file.write(encrypted_data)
os.rename(items, ex)
if FileExistsError:
os.system("clear")
os.system("cls")
parser = argparse.ArgumentParser(
"Encrypt and decryt files", usage="python enc.py <File> <Mode> <-e (Extension) (optional)>"
)
parser.add_argument(
"File", help="The file will be decrypt or encrypt"
)
parser.add_argument(
"Mode", help="You will decrypt or encrypt?"
)
parser.add_argument(
"-e","--Extension", help="The text after the .", required=False, default=".encrypted"
)
args = parser.parse_args()
extension = args.Extension
print(args.File)
print(args.Mode)
print(args.Extension)
if args.Mode == "encrypt":
generate_key()
k = get_key()
encript(args.File, k, extension)
if args.Mode=="decrypt":
k = get_key()
decrypt(args.File, k)