-
Notifications
You must be signed in to change notification settings - Fork 1
/
proj1.py
493 lines (430 loc) · 19.2 KB
/
proj1.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
from Crypto.Cipher import AES, ARC4, DES3
import base64
import codecs
import sys
import os.path
import os
import binascii
from colorama import init
from termcolor import cprint
from pyfiglet import figlet_format
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected # the big banner R O C K Y
cprint(figlet_format('R O C K Y', font='larry3d'), color='yellow', attrs=['bold'])
def banner(text, ch='-', length=39): # the small banner --- ---
spaced_text = ' %s ' % text
banner = spaced_text.center(length, ch)
print(banner)
text = 'ROCKY CRYPTO TOOL'
banner(text)
print("\n")
def ceaser_enc(message, key):
a = []
cipher = ''
char = 'a'
while char <= 'z':
a.append(char)
char = chr(ord(char) + 1)
for ch in message:
if ch.isalpha():
for i in range(26):
if ch == a[i]:
x = i + key
if x > 25:
x = x % 26
cipher += a[x]
else:
cipher += a[x]
else:
cipher += ' '
print("\n #|>#|># your Encrypted message: ", cipher, "\n")
def ceaser_dec(message, key):
a = []
text = ''
char = 'a'
while char <= 'z':
a.append(char)
char = chr(ord(char) + 1)
for ch in message:
if ch.isalpha():
for i in range(26):
if ch == a[i]:
x = i - key
if x < 0:
x = x % 26
text += a[x]
else:
text += a[x]
else:
text += ' '
print("\n #<|#<|# your Decrypted message: ", text, "\n")
def AES_enc_messege(message, key, iv):
if len(message) % 16 != 0:
message += b' ' * (16 - (len(message) % 16))
new = AES.new(key, AES.MODE_CBC, iv)
enc = new.encrypt(message)
encoding = base64.b64encode(enc)
print("\n #|>#|># your Encrypted message: ", codecs.decode(encoding, 'utf-8'), "\n")
def AES_dec_message(message, key, iv):
dec = base64.b64decode(message)
new = AES.new(key, AES.MODE_CBC, iv)
dy = new.decrypt(dec)
encoding = codecs.decode(dy, 'utf-8')
print("\n #<|#<|# your Decrypted message: ", encoding, "\n")
def AES_enc_files(file_name, key, iv):
file_size = str(os.path.getsize(file_name)).zfill(16).encode('ascii')
new_file = input("write the complete path where you want to save the file with the extension: ")
read_size = 1024
new = AES.new(key, AES.MODE_CBC, iv)
with open(file_name, 'rb') as f1:
with open(new_file, 'wb') as f2:
f2.write(file_size)
while True:
data = f1.read(read_size)
if len(data) == 0:
break
elif len(data) % 16 != 0:
data += b' ' * (16 - (len(data) % 16))
enc = new.encrypt(data)
f2.write(enc)
f2.close()
f1.close()
print("mission completed successfully\n")
def AES_dec_files(file_name, key, iv):
with open(file_name, 'rb') as f1:
new = AES.new(key, AES.MODE_CBC, iv)
new_file = input("write the complete path where you want to save file with the extension: ")
read_size = 1024
file_size = int(f1.read(16).decode('ascii').lstrip('0'))
with open(new_file, 'wb') as f2:
while True:
data = f1.read(read_size)
if len(data) == 0:
break
decrypt = new.decrypt(data)
f2.write(decrypt)
f2.truncate(file_size)
f2.close()
f1.close()
print("mission completed successfully\n")
def ARC4_enc_message(message, key):
if len(message) % 16 != 0:
message += b' ' * (16 - (len(message) % 16))
new = ARC4.new(key)
enc = new.encrypt(message)
encoding = base64.b64encode(enc)
print("\n #|>#|># your Encrypted message: ", codecs.decode(encoding, 'utf-8'), "\n")
def ARC4_dec_message(message, key):
dec = base64.b64decode(message)
new = ARC4.new(key)
dy = new.decrypt(dec)
encoding = codecs.decode(dy, 'utf-8')
print("\n #<|#<|# your Decrypted message: ", encoding, "\n")
def ARC4_enc_files(file_name, key):
file_size = str(os.path.getsize(file_name)).zfill(16).encode('ascii')
new_file = input("write the complete path where you want to save the file with the extension: ")
read_size = 1024
new = ARC4.new(key)
with open(file_name, 'rb') as f1:
with open(new_file, 'wb') as f2:
f2.write(file_size)
while True:
data = f1.read(read_size)
if len(data) == 0:
break
elif len(data) % 16 != 0:
data += b' ' * (16 - (len(data) % 16))
enc = new.encrypt(data)
f2.write(enc)
f2.close()
f1.close()
print("mission completed successfully\n")
def ARC4_dec_files(file_name, key):
with open(file_name, 'rb') as f1:
new = ARC4.new(key)
new_file = input("write the complete path where you want to save file with the extension: ")
read_size = 1024
file_size = int(f1.read(16).decode('ascii').lstrip('0'))
with open(new_file, 'wb') as f2:
while True:
data = f1.read(read_size)
if len(data) == 0:
break
decrypt = new.decrypt(data)
f2.write(decrypt)
f2.truncate(file_size)
f2.close()
f1.close()
print("mission completed successfully\n")
def DES3_enc_message(message, key, iv):
if len(message) % 16 != 0:
message += b' ' * (16 - (len(message) % 16))
new = DES3.new(key, DES3.MODE_CBC, iv)
enc = new.encrypt(message)
encoding = base64.b64encode(enc)
print("\n #|>#|># your Encrypted message: ", codecs.decode(encoding, 'utf-8'), "\n")
def DES3_dec_message(message, key, iv):
dec = base64.b64decode(message)
new = DES3.new(key, DES3.MODE_CBC, iv)
dy = new.decrypt(dec)
encoding = codecs.decode(dy, 'utf-8')
print("\n #<|#<|# your Decrypted message: ", encoding, "\n")
def DES3_enc_files(file_name, key, iv):
new_file = input("write the complete path where you want to save the file with the extension: ")
read_size = 1024
new = DES3.new(key, DES3.MODE_CBC, iv)
with open(file_name, 'rb') as f1:
with open(new_file, 'wb') as f2:
while True:
data = f1.read(read_size)
if len(data) == 0:
break
elif len(data) % 16 != 0:
data += b' ' * (16 - (len(data) % 16))
enc = new.encrypt(data)
encode = base64.b64encode(enc)
f2.write(encode)
f2.close()
f1.close()
print("mission completed successfully\n")
def DES3_dec_files(file_name, key, iv):
new = DES3.new(key, DES3.MODE_CBC, iv)
new_file = input("write the complete path where you want to save the file with the extension: ")
read_size = 1024
with open(file_name, 'rb') as f1:
with open(new_file, 'wb') as f2:
while True:
data = f1.read(read_size)
decode = base64.b64decode(data)
if len(data) == 0:
break
decrypt = new.decrypt(decode)
f2.write(decrypt)
f2.close()
f1.close()
print("mission completed successfully\n")
def random():
print("key == ", binascii.hexlify(os.urandom(8)).decode('utf-8'))
print("iv == ", binascii.hexlify(os.urandom(8)).decode('utf-8'), "\n")
while True:
x = input("[H] >> help\n[1] >> file\n[2] >> message\n[3] >> generate random key & iv\n[0] >> exit\n choose: ")
if x == 'exit' or x == '0':
print("\n[ Thanks for your time ^_^ BYE, ]")
break
# ==============================================================================================================
# Encryption and Decryption of messages
elif x == 'message' or x == '2':
print("\n1- AES\n2- ARC4\n3- DES3\n4- CEASER")
y = int(input("\nwrite the number of the cipher : "))
# AES-Cipher
# ------------------------------------------
if y == 1:
a = input("write 'E' for AES-Encryption or 'D' for AES-Decryption: ")
if a == 'E':
m = codecs.encode(input("write the message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
try:
AES_enc_messege(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv size, they must be 16[AES-128] size or 24[AES-192] or "
"32[AES-256]")
elif a == 'D':
m = codecs.encode(input("write the Decrypted message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
try:
AES_dec_message(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv size, they must be 16[AES-128] size or 24[AES-192] or "
"32[AES-256]")
# --------------------------------------------------------------------------------------------------
# ARC4-Cipher
# ------------------------------------------
elif y == 2:
a = input("write 'E' for ARC4-Encryption or 'D' for ARC4-Decryption: ")
if a == 'E':
m = codecs.encode(input("write the message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
try:
ARC4_enc_message(m, k)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key >> ARC4 key size must be greater than 8 byte ")
elif a == 'D':
m = codecs.encode(input("write the Decrypted message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
try:
ARC4_dec_message(m, k)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key >> ARC4 key size must be greater than 8 byte ")
# -------------------------------------------------------------------------------------------------
# DES3-Cipher
# --------------------------------------------
elif y == 3:
a = input("write 'E' for DES3-Encryption or 'D' for DES3-Decryption: ")
if a == 'E':
m = codecs.encode(input("write the message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
try:
DES3_enc_message(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv >> DES3 key must be 16 or 24 bytes & iv must be 8 bytes")
elif a == 'D':
m = codecs.encode(input("write the Decrypted message: "), 'utf-8')
while True:
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
try:
DES3_dec_message(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv >> DES3 key must be 16 or 24 bytes & iv must be 8 bytes")
# ---------------------------------------------------------------------------------------------------
# CEASER-cipher
# ----------------------------------------------
elif y == 4:
a = input("write 'E' for CEASER-Encryption or 'D' for CEASER-Decryption: ")
if a == 'E':
m = input("write the message: ")
k = int(input("write your key: "))
ceaser_enc(m, k)
elif a == 'D':
m = input("write the cipher message: ")
k = int(input("write your key: "))
ceaser_dec(m, k)
else:
print("wrong choose, please choose again")
continue
# =====================================================================================================================
# Encryption and Decryption of files
elif x == 'file' or x == '1':
print("1- AES\n2- ARC4\n3- DES3")
y = int(input("write the number of the cipher : "))
# AES-Cipher
# ------------------------------------------
if y == 1:
a = input("write 'E' for AES-Encryption or 'D' for AES-Decryption: ")
if a == 'E':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
AES_enc_files(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv size, they must be 16[AES-128] size or 24[AES-192] or "
"32[AES-256]")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
elif a == 'D':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
AES_dec_files(m, k, v)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv size, they must be 16[AES-128] size or 24[AES-192] or "
"32[AES-256]")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
# -----------------------------------------------------------------------------------
# ARC4-Cipher
# ------------------------------------------
elif y == 2:
a = input("write 'E' for ARC4-Encryption or 'D' for ARC4-Decryption: ")
if a == 'E':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
ARC4_enc_files(m, k)
break
except ValueError:
print(" [XXX]/\/\ Wrong in key >> ARC4 key size must be greater than 8 byte ")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
elif a == 'D':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
ARC4_dec_files(m, k)
except ValueError:
print(" [XXX]/\/\ Wrong in key >> ARC4 key size must be greater than 8 byte ")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
# ------------------------------------------------------------------------------
# DES3-Cipher
# --------------------------------------------
elif y == 3:
a = input("write 'E' for DES3-Encryption or 'D' for DES3-Decryption: ")
if a == 'E':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
DES3_enc_files(m, k, v)
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv >> DES3 key must be 16 or 24 bytes & iv must be 8 bytes")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
elif a == 'D':
while True:
try:
m = input("write the path of the file: ")
k = codecs.encode(input("write your key: "), 'utf-8')
v = codecs.encode(input("write your iv: "), 'utf-8')
DES3_dec_files(m, k, v)
except ValueError:
print(" [XXX]/\/\ Wrong in key or iv >> DES3 key must be 16 or 24 bytes & iv must be 8 bytes")
except FileNotFoundError:
print(" [ ... sorry, file not found ... ] ")
else:
print("wrong choose, please choose again")
continue
# --------------------------------------------------------------------------------
# generating random key and iv
# -----------------------------------------------------
elif x == 'generate' or x == '3':
random()
# --------------------------------------------------------------------------------
# HELP section
# -------------------------------------------------------
elif x == 'help' or x == 'H':
banner(text)
print("# ROCKY tool for Encryption/Decryption messages or files\nCreated by: EXZANDAR")
print("# NOTES: \n\n* this tool use BASE64 encoding and decoding \n** the 'KEY' & 'IV' for AES-Cipher should be "
"16 for [AES-128] or 24 for [AES-192] or 32 for [AES-256]"
"\n*** when you use DES3 Cipher the 'IV' size is '8' "
"\n**** when you encrypt/decrypt file, write the file with its extension\n"
"*** you can generate random key &"
" iv to use them\n** when you use CEASER cipher in Encrypt/Decrypt messages the valid range key [0:25]\n"
"* you "
"can send me feedback at hema66458@gmail.com\n")
print("# EXAMPLE: 'of encryption/decryption messages using AES Cipher' "
"\nmessage: 'hello world' key: '1234567891234567' iv: '9876543219876543' ")
print(
"\n# EXAMPLE: 'of encryption/decryption file using AES cipher'\nfile path: F:\\file.txt\n"
"key: '1234567891234567'"
"\niv: '9876543219876543'")
print(" >>> the encrypted message will be look like 'e3vtzoQyySWdanELjYV2Ng==' <<< ")
print("\n[ finally hope you use it carefully ]\n")
banner(text)
else:
print("wrong choose, please choose again")
continue
# -----------------------------------------------------------------------------