-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscript.py
430 lines (231 loc) · 9.34 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
'''
Symmetric Cryptographic Encryption and Decryption Tool in Python (AES)
Repo Owner :- PrajwalCyberGod - https://github.com/PrajwalCyberGod/PrajwalCyberGod
'''
# Imports
from Crypto import Random
from Crypto.Cipher import AES
import os
import os.path
from tqdm import tqdm
from termcolor import colored,cprint
import string
import random
# Defined Functions
class Encryptor:
def __init__(self, key):
self.key = key
def pad(self, s):
return s + b"\0" * (AES.block_size - len(s) % AES.block_size)
def encrypt(self, message, key):
message = self.pad(message)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(message)
def encrypt_file(self, file_name):
with open(file_name, 'rb') as fo:
plaintext = fo.read()
enc = self.encrypt(plaintext, self.key)
with open(file_name + ".enc", 'wb') as fo:
fo.write(enc)
os.remove(file_name)
def decrypt(self, ciphertext, key):
iv = ciphertext[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
plaintext = cipher.decrypt(ciphertext[AES.block_size:])
return plaintext.rstrip(b"\0")
def decrypt_file(self, file_name):
with open(file_name, 'rb') as fo:
ciphertext = fo.read()
dec = self.decrypt(ciphertext, self.key)
with open(file_name[:-4], 'wb') as fo:
fo.write(dec)
os.remove(file_name)
def getAllFiles(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
dirs = []
for dirName, subdirList, fileList in os.walk(dir_path):
for fname in fileList:
if (fname != 'script.py' and fname != 'password.txt.enc'):
dirs.append(dirName + "\\" + fname)
return dirs
def encrypt_all_files(self):
dirs = self.getAllFiles()
for file_name in dirs:
self.encrypt_file(file_name)
def decrypt_all_files(self):
dirs = self.getAllFiles()
for file_name in dirs:
self.decrypt_file(file_name)
space_count = 60 * ' '
key = b'[EX\xc8\xd5\xbfI{\xa2$\x05(\xd5\x18\xbf\xc0\x85)\x10nc\x94\x02)j\xdf\xcb\xc4\x94\x9d(\x9e'
enc = Encryptor(key)
clear = lambda: os.system('cls')
# Main program
if os.path.isfile('password.txt.enc'):
while True:
# Authentication
clear()
cprint('\n{}*** Encryption And Decryption Tool (AES) ***'.format(space_count),'red',attrs=['bold'])
cprint('\n{}*** Programmed by Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint('\n\tEnter password: ','green', attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold','blink'], on_color='on_white')
password = str(input('\n\t'))
enc.decrypt_file("password.txt.enc")
p = ''
with open("password.txt", "r") as f:
p = f.readlines()
if p[0] == password:
enc.encrypt_file("password.txt")
cprint('\n\tAuthentication Completed','green', attrs=['bold'])
break
else:
enc.encrypt_file("password.txt")
cprint('\n\tINCORRECT PASSWORD.....!','red', attrs=['bold'])
exit()
while True:
# Choice Menu
clear()
cprint('\n{}*** Encryption And Decryption Tool (AES) ***'.format(space_count),'red',attrs=['bold'])
cprint('\n{}*** Programmed by Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint('\n\t1. Press 1 to Encrypt a file','cyan', attrs=['bold'])
cprint('\n\t2. Press 2 to Decrypt a file','yellow', attrs=['bold'])
cprint('\n\t3. Press 3 to Encrypt all files in the directory','green', attrs=['bold'])
cprint('\n\t4. Press 4 to Decrypt all files in the directory','magenta', attrs=['bold'])
cprint('\n\t5. Press 5 to Exit','red',attrs=['bold'])
cprint('\n\tEnter your choice (in integer format) :','cyan',attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold','blink'], on_color='on_white')
choice = int(input('\n\t'))
clear()
# To ENCRYPT the file
if choice == 1:
logo = '''
___ _ _
| __|_ _ __ _ _ _ _ _ __| |_(_)___ _ _
| _|| ' \/ _| '_| || | '_ \ _| / _ \ ' \
|___|_||_\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
'''
cprint(logo,'cyan',attrs=['bold'])
cprint("\n\tEnter name of file to encrypt : ",'cyan', attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold','blink'], on_color='on_white')
enc.encrypt_file(str(input('\n\t')))
cprint('\n\tEncryption done Sucessfully.....!','green',attrs=['bold'])
cprint('\n\tDo you want to do it again (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
continue
else:
break
# To DECRYPT the file
elif choice == 2:
logo = '''
___ _ _
| \ ___ __ _ _ _ _ _ __| |_(_)___ _ _
| |) / -_) _| '_| || | '_ \ _| / _ \ ' \
|___/\___\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
'''
cprint(logo,'yellow',attrs=['bold'])
cprint("\n\tEnter name of file to decrypt : ",'yellow', attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold','blink'], on_color='on_white')
enc.decrypt_file(str(input('\n\t')))
cprint('\n\tDecryption done Sucessfully.....!','green',attrs=['bold'])
cprint('\n\tDo you want to do it again (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
continue
else:
break
# To ENCRYPT the directory
elif choice == 3:
logo = '''
___ _ _
| __|_ _ __ _ _ _ _ _ __| |_(_)___ _ _
| _|| ' \/ _| '_| || | '_ \ _| / _ \ ' \
|___|_||_\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
'''
cprint(logo,'green',attrs=['bold'])
enc.encrypt_all_files()
cprint('\n\tEncryption done Sucessfully.....!','green',attrs=['bold'])
cprint('\n\tDo you want to do it again (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
continue
else:
break
# To DECRYPT the directory
elif choice == 4:
logo = '''
___ _ _
| \ ___ __ _ _ _ _ _ __| |_(_)___ _ _
| |) / -_) _| '_| || | '_ \ _| / _ \ ' \
|___/\___\__|_| \_, | .__/\__|_\___/_||_|
|__/|_|
'''
cprint(logo,'magenta',attrs=['bold'])
enc.decrypt_all_files()
cprint('\n\tDecryption done Sucessfully.....!','green',attrs=['bold'])
cprint('\n\tDo you want to do it again (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
continue
else:
break
# To close the program
elif choice == 5:
exit()
# When user input has an error
else:
cprint('\n\tPlease select a valid option.....!','red',attrs=['bold'])
break
# Password setup
else:
while True:
cprint('\n{}*** Encryption And Decryption Tool (AES) ***'.format(space_count),'red',attrs=['bold'])
cprint('\n{}*** Programmed by Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint("\n\tSetting up stuff.....",'yellow',attrs=['bold'])
cprint("\n\tDo you want to generate a random password (y/n) : ",'red',attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold'], on_color='on_white')
random_pass = input('\n\t')
if (random_pass.lower() == 'y'):
# For random password generator
s1 = string.ascii_lowercase
s2 = string.ascii_uppercase
s3 = string.digits
s4 = string.punctuation
cprint("\n\tEnter password length: ",'yellow', attrs=['bold'])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold'], on_color='on_white')
plen = int(input('\n\t'))
s = []
s.extend(list(s1))
s.extend(list(s2))
s.extend(list(s3))
s.extend(list(s4))
password = "".join(random.sample(s, plen))
cprint('\n\tPassword is saved Sucessfully.....!','green',attrs=['bold'])
cprint('\n\tYour password is :','magenta',attrs=['bold'])
cprint('\n\t'+password,'green',attrs=['bold'])
cprint('\n\tPlease save it SECURELY.....!','cyan',attrs=['bold'])
cprint("\n\tPlease restart the program to complete the setup.....",'yellow',attrs=['bold'])
break
else:
# For manual password setup
cprint("\n\tEnter a password that will be used for decryption : ",'cyan',attrs=['bold',])
cprint('\n\t~CRYPTO : ','blue', attrs=['bold'], on_color='on_white')
password = str(input('\n\t'))
cprint("\n\tConfirm password: ",'magenta',attrs=['bold'])
cprint('\n\t~CRYPTO :','blue', attrs=['bold'], on_color='on_white')
repassword = str(input('\n\t'))
if password == repassword:
cprint('\n\tPassword is saved Sucessfully.....!','green',attrs=['bold'])
cprint("\n\tPlease restart the program to complete the setup.....",'yellow',attrs=['bold'])
break
else:
cprint("\n\tPasswords Mismatched.....!",'red', attrs=['bold'])
exit()
f = open("password.txt", "w+")
f.write(password)
f.close()
enc.encrypt_file("password.txt")