-
Notifications
You must be signed in to change notification settings - Fork 0
/
fernet_encryption.py
267 lines (252 loc) · 8.87 KB
/
fernet_encryption.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import os
from cryptography.fernet import Fernet
files = []
en_files = []
de_files = []
curennt_path = os.getcwd()
key = None
def showbanner():
print("""
#####################################################################
____________
____ | |
| | | O |
__|____ __|__ ___|_____ |___
| ____ __ |____ ____ | | | |
| |____| / | | |____| | | ____________| |
| |____ | | | |____ \ | | |
| \__ |___| ________|
| | |
en-/decryptor | O |
|____________|
by chimera83795
#####################################################################
!!!Warning! This is for educational porpuse only!!!
Doesn't use this programm against files without permission!
The author don't take any resonsability for things,
that are done with this programm!
Also be aware, that files, that are encrypted with this programm,
could be encrypted, locked or not accessable forever!
It's recommanded to only encrypt copies!
""")
def open_key():
global key
key_name = input("[*]Please enter the name of the .key file: ")
key_path = input("[*]Please enter the folder of the key: ")
try:
os.chdir(key_path)
with open(key_name, 'rb') as thekey:
key = thekey.read()
return key
except:
print("[-]Can't access the key")
open_key()
def encrypt(path, target_files):
global files, key
try:
os.chdir(path)
except:
print("[-]Please enter a valid path")
encrypt_setup()
number = 0
for file in os.listdir():
for target_file in target_files:
if file == target_file and os.path.isfile(file):
files.append(file)
number += 1
key_choice = input("[*]Use a already created key.key file?[y/n] ")
if key_choice == 'y':
open_key()
elif key_choice == 'n':
os.chdir(curennt_path)
key = Fernet.generate_key()
with open("thekey.key" , "wb") as thekey:
thekey.write(key)
print("[+]Successfully saved the key as thekey.key in current workingdirectory ")
os.chdir(path)
for file in files:
try:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_encrypted = Fernet(key).encrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_encrypted)
print("[+]Successfully encrypted " + file)
except:
print("[-]" + file + " wasn't encrypted")
print("[*]Encryption finished")
files = []
input()
def decrypt(path, target_files):
global files, key
try:
os.chdir(path)
except:
print("[-]Please enter a valid path")
decrypt_setup()
open_key()
number = 0
os.chdir(path)
for file in os.listdir():
for target_file in target_files:
if file == target_file and os.path.isfile(file):
files.append(file)
number += 1
os.chdir(path)
for file in files:
try:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_decrypted = Fernet(key).decrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_decrypted)
print("[+]Successfully decrypted " + file)
except:
print("[-]" + file + " wasn't decrypted")
print("[*]Decryption finished")
files = []
input()
def encrypt_all(path):
global files, curennt_path, key
try:
os.chdir(path)
except:
print("[-]Please enter a valid path")
encrypt_setup()
number = 0
for file in os.listdir():
files.append(file)
number += 1
key_choice = input("[*]Use a already created key.key file?[y/n] ")
if key_choice == 'y':
open_key()
elif key_choice == 'n':
os.chdir(curennt_path)
key = Fernet.generate_key()
with open("thekey.key" , "wb") as thekey:
thekey.write(key)
print("[+]Successfully saved the key as thekey.key in current workingdirectory ")
os.chdir(path)
for file in files:
try:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_encrypted = Fernet(key).encrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_encrypted)
print("[+]Successfully encrypted " + file)
except:
print("[-]" + file + " wasn't encrypted")
print("[*]Encryption finished")
files = []
input()
def decrypt_all(path):
global files, curennt_path, key
number = 0
try:
os.chdir(path)
except:
print("[-]Please enter a valid path")
decrypt_setup()
for file in os.listdir():
if file == "fernet_encryption.exe" or file == "thekey.key":
continue
if os.path.isfile(file):
files.append(file)
number += 1
open_key()
os.chdir(path)
for file in files:
try:
with open(file, "rb") as thefile:
contents = thefile.read()
contents_decrypted = Fernet(key).decrypt(contents)
with open(file, "wb") as thefile:
thefile.write(contents_decrypted)
print("[+]Successfully decrypted " + file)
except:
print("[-]" + file + " wasn't decrypted")
print("[*]Decryption finished")
files = []
input()
def encrypt_setup():
global en_files
en_path = input("encrypter>[*]Enter target path of the folder: ")
if en_path == 'options' or en_path == 'showoptions':
print("""
number = decrypts/encrypts x target files
all = decrypts/encrypts all files in the folder
""")
encrypt_setup()
en_number = input("encrypter>[*]How many files do you want to encrypt? ")
if en_number == 'all':
encrypt_all(en_path)
elif en_number == 'options' or en_number == 'show options':
print("""
number = decrypts/encrypts x target files
all = decrypts/encrypts all files in the folder
""")
encrypt_setup()
else:
try:
en_number = int(en_number)
except:
print("[-]Please enter a number")
encrypt_setup()
for i in range(en_number):
en_file = input("[*]Enter a file: ")
en_files.append(en_file)
encrypt(en_path, en_files)
def decrypt_setup():
global de_files
de_path = input("decrypter>[*]Enter target path of the folder: ")
if de_path == 'options' or de_path == 'show options':
print("""
number = decrypts/encrypts x target files
all = decrypts/encrypts all files in the folder
""")
decrypt_setup()
de_number = input("decrypter>[*]How many files do you want to decrypt? ")
if de_number == 'all':
decrypt_all(de_path)
elif de_number == 'options' or de_number == 'show options':
print("""
number = decrypts/encrypts x target files
all = decrypts/encrypts all files in the folder
""")
decrypt_setup()
else:
try:
de_number = int(de_number)
except:
print("[-]Please enter a number")
decrypt_setup()
for i in range(de_number):
de_file = input("[*]Enter a file: ")
de_files.append(de_file)
decrypt(de_path, de_files)
def main():
menu = input("#fernet_cryptography$>")
if menu == 'help' or menu == '--help' or menu == '-help' or menu == '-h':
print("""
Opening help page...
help = shows this help-page
encrypt = opens the encrypter
decrypt = opens the decrypter
exit = exits the programm
options = shows the options for en-/decryption
""")
main()
elif menu == 'encrypt' or menu == '-encrypt':
encrypt_setup()
elif menu == 'decrypt' or menu == '-decrypt':
decrypt_setup()
elif menu == 'exit' or menu == '-exit':
print("[*]Exiting programm...")
exit()
else:
print("\n[-]Command not found\n")
main()
if __name__ == "__main__":
showbanner()
main()