-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimnigma.py
executable file
·335 lines (315 loc) · 14.1 KB
/
simnigma.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
#!/usr/bin/python3
import sys
import os
import glob
import time
from modules.__tools_single import bcolors
from modules.test_print import test_print
from modules.tools import (encrypt, decrypt, load_file_all, save_file_all, convert_str_to_list, convert_list_to_str,
create_random_64b_key, check_64b_key,
create_rotors, check_rand_rotors,
print_long, show_help, printd)
version = '5.0.0'
options = sys.argv + [' ',]
options[0] = options[0][1:]
options_all = ('-c', '-d', '-k', '-r', '-K', '-R', '-v', '-f', '--verbose', '-t', '--tests', '-h', '--help', '-V' ,'--version',
'-s', '--silent')
max_print_length = int(os.popen('stty size', 'r').read().split()[1]) - 10
min_print_length = 15
space = 45
debug = False
os.system('setterm -cursor off')
files_to_encrypt = []
files_to_decrypt = []
key_name_load = ''
rotors_name_load = ''
key_name_save = ''
key_size = 0
rotors_name_save = ''
rotors_number = 0
show = False
only_screen = False
force = False
key = ''
rotors = []
# extracting options from the command line
printd( options, debug=debug)
if '-c' in options:
for file_my in options[options.index('-c') + 1:]:
if file_my[0] != '-' and file_my[0] != ' ':
files_to_encrypt.append(file_my)
else:
break
del options[options.index('-c') + 1: options.index('-c') + len(files_to_encrypt) + 1]
options.remove('-c')
if '-d' in options:
for file_my in options[options.index('-d') + 1:]:
if file_my[0] != '-' and file_my[0] != ' ':
files_to_decrypt.append(file_my)
else:
break
del options[options.index('-d') + 1: options.index('-d') + len(files_to_decrypt) + 1]
options.remove('-d')
if '-k' in options:
if options[options.index('-k') + 1][0] != '-':
key_name_load += options[options.index('-k') + 1]
del options[options.index('-k') + 1]
options.remove('-k')
if '-r' in options:
if options[options.index('-r') + 1][0] != '-':
rotors_name_load += options[options.index('-r') + 1]
del options[options.index('-r') + 1]
options.remove('-r')
if '-v' in options or '--verbose' in options:
show = True
try: options.remove('-v')
except: options.remove('--verbose')
if '-s' in options or '--silent' in options:
only_screen = True
try: options.remove('-s')
except: options.remove('--silent')
if '-K' in options:
if options[options.index('-K') + 1][0] != '-':
key_name_save += options[options.index('-K') + 1]
try:
key_size = options[options.index('-K') + 2]
key_size = int(key_size)
except:
show_help('Invalid name or size of key')
del options[options.index('-K') + 2]
del options[options.index('-K') + 1]
options.remove('-K')
if '-R' in options:
if options[options.index('-R') + 1][0] != '-':
rotors_name_save += options[options.index('-R') + 1]
try:
rotors_number = options[options.index('-R') + 2]
rotors_number = int(rotors_number)
except:
show_help('Invalid number of rotors')
del options[options.index('-R') + 2]
del options[options.index('-R') + 1]
options.remove('-R')
if '-h' in options or '--help' in options:
show_help('')
if '-V' in options or '--version' in options:
printd(options, show)
if len(sys.argv) != 2:
show_help("Invalid options")
else:
printd(version)
print("Simnigma version", version)
exit()
if '-f' in options:
force = True
options.remove('-f')
# check options from cmd
printd( options, files_to_encrypt, files_to_decrypt, key_name_load, key_name_save, key_size)
printd( rotors_name_load, rotors_name_save, rotors_number, only_screen, show, debug=debug)
if len(sys.argv) == 1:
show_help("No options")
if len(options) > 2:
show_help("Invalid options")
if '-c' in sys.argv and files_to_encrypt == []:
show_help("File to encrypt or to save if -s")
if '-d' in sys.argv and files_to_decrypt == []:
show_help("File to decrypt or to load if -s")
if '-k' in sys.argv and key_name_load == ' ':
show_help("Name of key to load")
if '-r' in sys.argv and rotors_name_load == ' ':
show_help("Name of rotors to load")
if ('-c' in sys.argv or '-d' in sys.argv) and ('-K' in sys.argv or '-R' in sys.argv):
show_help("Too many options, at the same time you can't encrypt/decrypt and create key or rotors")
if ('-K' in sys.argv or '-R' in sys.argv) and ('-s') in sys.argv:
show_help("Too many options, at the same time you can't create key or rotors in silent mode")
if key_name_save and key_size > 10000 and not force:
show_help("Too long key [limit 10 000], if you have enough time put -f in options")
if rotors_name_save and rotors_number > 100 and not force:
show_help("Too many rotors [limit 100], if you have enough time put -f in options")
if show and not [opt for opt in sys.argv if opt in ['-c', '-d', '-k', '-r', '-K', '-R']]:
show_help("Verbose works only in ['-c', '-d', '-k', '-r', '-K', '-R']")
# loading the key
if key_name_load:
printd(key_name_load, debug=debug)
if key_name_load[0] == '/':
key = load_file_all(key_name_load, 'key', True)
elif key_name_load[0:2] == '..':
key = load_file_all(os.getcwd()[:os.getcwd().rfind('/')] + key_name_load[2:], 'key', True)
elif key_name_load[0:2] == './':
key = load_file_all(os.getcwd() + key_name_load[1:], 'key', True)
elif key_name_load[-4:] == '.key':
key = load_file_all(os.getcwd() + '/' + key_name_load, 'key', True)
elif key_name_load:
key = load_file_all(options[0][:options[0].rfind('/')] + '/keys/' + key_name_load, 'key', True)
elif ('-c' in sys.argv) or ('-d' in sys.argv):
if sys.platform == 'linux':
if glob.glob("/media/**/*.key", recursive=True):
key_name_load = max(glob.glob("/media/**/*.key", recursive=True), key=os.path.getatime)
key = load_file_all(key_name_load, 'key', True)
if (glob.glob(options[0][:options[0].rfind('/') + 1] + 'keys/*')) and not key:
key_name_load = max(glob.glob(options[0][:options[0].rfind('/') + 1] + 'keys/*'), key=os.path.getatime)
key = load_file_all(key_name_load, 'key', True)
printd(key, debug=debug)
if not rotors_name_save and not key_name_save and key:
if check_64b_key(key):
if show==False: print("Key test:" + " " * (space - 6) + "[" + bcolors.BOLD + "PASS" + bcolors.ENDC + "]")
else:
show_help("Key {} are broken, make new or del broken".format(key_name_load))
elif not rotors_name_save and not key_name_save :
show_help("You don't have any keys, make or connect USB with key")
# loading of rotors
if rotors_name_load:
printd(rotors_name_load, debug=debug)
if rotors_name_load[0] == '/':
rotors = load_file_all(rotors_name_load, 'rotors_from_one_file', True)
elif rotors_name_load[0:2] == '..':
rotors = load_file_all(os.getcwd()[:os.getcwd().rfind('/')] + rotors_name_load[2:], 'rotors_from_one_file', True)
elif rotors_name_load[0:2] == './':
rotors = load_file_all(os.getcwd() + rotors_name_load[1:], 'rotors_from_one_file', True)
elif rotors_name_load[-4:] == '.rotor':
rotors = load_file_all(os.getcwd() + '/' + rotors_name_load, 'rotors_from_one_file', True)
elif rotors_name_load:
rotors = load_file_all(options[0][:options[0].rfind('/')] + '/rotors/' + rotors_name_load, 'rotors_from_one_file', True)
elif ('-c' in sys.argv) or ('-d' in sys.argv):
if sys.platform == 'linux':
if glob.glob("/media/**/*.rotors", recursive=True):
rotors_name_load = max(glob.glob("/media/**/*.rotors", recursive=True), key=os.path.getatime)
rotors = load_file_all(rotors_name_load, 'rotors_from_one_file', True)
if (glob.glob(options[0][:options[0].rfind('/') + 1] + 'rotors/*.rotors')) and not rotors:
rotors_name_load = max(glob.glob(options[0][:options[0].rfind('/') + 1] + 'rotors/*.rotors'), key=os.path.getatime)
rotors = load_file_all(rotors_name_load, 'rotors_from_one_file', True)
printd(rotors, debug=debug)
if not rotors_name_save and not key_name_save and rotors:
if check_rand_rotors(rotors):
if min(rotors[0]) == 0 and max(rotors[0]) == 255 and min(rotors[0].values()) == 0 and max(rotors[0].values()) == 255:
if show==False: print("Randomness and integrity rotor test:" + " " * (space - 33) + "[" + bcolors.BOLD + "PASS" + bcolors.ENDC + "]")
else:
show_help("Rotors {} are broken, make new or del broken".format(rotors_name_load))
elif not rotors_name_save and not key_name_save:
show_help("You don't have any rotors, make or connect USB with rotors")
# encrypt file
if files_to_encrypt and not only_screen:
printd(files_to_encrypt, debug=debug)
for file_my in files_to_encrypt[:]:
if not os.path.isfile(file_my):
print(bcolors.IMPORTANT + 'Warning: "{}" this isn\'t a file'.format(file_my) + bcolors.ENDC)
files_to_encrypt.remove(file_my)
for file_my in files_to_encrypt[:]:
if os.stat(str(os.path.abspath('') + '/' + file_my)).st_size > 0:
files_to_encrypt[files_to_encrypt.index(file_my)] = str(os.path.abspath('') + '/' + file_my)
else:
print(bcolors.IMPORTANT + "Warning: Empty file {}".format(file_my) + bcolors.ENDC)
files_to_encrypt.remove(file_my)
for file_my in files_to_encrypt:
text_before = load_file_all(file_my, 'file', True)
text_encrypt = encrypt(rotors, key, text_before, show=True)
save_file_all(file_my + '.enc', text_encrypt, 'file', True)
if show: test_print(rotors=rotors, key_enc=key, text_before=text_before, text_encrypt=text_encrypt,
show_all=True, show_uni=True, max_print_length=max_print_length, min_print_length=min_print_length,
space=space)
if files_to_encrypt and only_screen:
printd(files_to_encrypt, debug=debug)
for file_my in files_to_encrypt:
print("Enter/Paste your content. Ctrl-D or Ctrl-C to save it.")
os.system('setterm -cursor on')
text_before = ''
while True:
try:
if text_before: text_before += '\n'
text_before += input('<--- ')
except KeyboardInterrupt:
break
except EOFError:
break
if len(text_before) == 0: show_help('Text is empty')
os.system('setterm -cursor off')
text_before = convert_str_to_list(text_before)
text_encrypt = encrypt(rotors, key, text_before, show=True)
save_file_all(file_my + '.enc', text_encrypt, 'file', True)
if show: test_print(rotors=rotors, key_enc=key, text_before=text_before, text_encrypt=text_encrypt,
show_all=True, show_uni=True)
# decrypt file
if files_to_decrypt and not only_screen:
printd(files_to_decrypt, debug=debug)
for file_my in files_to_decrypt[:]:
if (not os.path.isfile(file_my) or (file_my[-4:] != '.enc')):
print(bcolors.IMPORTANT + 'Warning: "{}" this isn\'t a .enc file or doesn\' exist'.format(file_my) + bcolors.ENDC)
files_to_decrypt.remove(file_my)
for file_my in files_to_decrypt[:]:
if os.stat(str(os.path.abspath('') + '/' + file_my)).st_size > 0:
files_to_decrypt[files_to_decrypt.index(file_my)] = str(os.path.abspath('') + '/' + file_my)
else:
print(bcolors.IMPORTANT + "Warning: Empty file {}".format(file_my) + bcolors.ENDC)
files_to_decrypt.remove(file_my)
for file_my in files_to_decrypt:
text_encrypt = load_file_all(file_my, 'file', True)
text_decrypt = decrypt(rotors, key, text_encrypt, show=True)
save_file_all(file_my + '.dec', text_decrypt, 'file', True)
if show: test_print(rotors=rotors, key_enc=key, text_encrypt=text_encrypt, text_decrypt=text_decrypt,
show_all=True, show_uni=True, max_print_length=max_print_length, min_print_length=min_print_length,
space=space)
if files_to_decrypt and only_screen :
printd(files_to_decrypt, debug=debug)
for file_my in files_to_decrypt[:]:
if (not os.path.isfile(file_my) or (file_my[-4:] != '.enc')):
os.system('setterm -cursor on')
exit(bcolors.WARNING + '"{}" this isn\'t a .enc file or doesn\' exist'.format(file_my) + bcolors.ENDC)
for file_my in files_to_decrypt[:]:
if os.stat(str(os.path.abspath('') + '/' + file_my)).st_size > 0:
files_to_decrypt[files_to_decrypt.index(file_my)] = str(os.path.abspath('') + '/' + file_my)
else:
os.system('setterm -cursor on')
exit(bcolors.IMPORTANT + "Warning: Empty file {}".format(file_my) + bcolors.ENDC)
os.system('clear')
for file_my in files_to_decrypt:
text_encrypt = load_file_all(file_my, 'file', True)
text_decrypt = decrypt(rotors, key, text_encrypt, show=True)
if show:
test_print(rotors=rotors, key_enc=key, text_encrypt=text_encrypt, text_decrypt=text_decrypt,
show_all=True, show_uni=True)
text_decrypt = convert_list_to_str(text_decrypt[:-1])
text_decrypt = text_decrypt.split('\n')
for line in text_decrypt:
print('---> ', line)
for s in range(10, -1, -1):
print('\rIn ' + bcolors.WARNING + bcolors.BOLD + '{}s '.format(s) + bcolors.ENDC + 'message and file will be destroyed ', end='')
time.sleep(1)
for _ in range(1000): print('\n')
os.system('rm ' + file_my)
os.system('clear')
# creating and saving key and rotors
if key_name_save:
printd(key_name_save, key_size, debug=debug)
if key_name_save[0] == '/':
pass
elif key_name_save[0:2] == '..':
key_name_save = os.getcwd()[:os.getcwd().rfind('/')] + key_name_save[2:]
elif key_name_save[0:2] == './':
key_name_save = os.getcwd() + key_name_save[1:]
elif key_name_save[-4:] == '.key':
key_name_save = os.getcwd() + '/' + key_name_save
elif key_name_save:
key_name_save = options[0][:options[0].rfind('/')] + '/keys/' + key_name_save
key = create_random_64b_key(key_size, max_print_length=max_print_length, show=show)
print('Key created: ', key_name_save[key_name_save.rfind('/') + 1:], str(key_size) + " bit")
save_file_all(key_name_save, key, 'key', show=True)
printd(key, debug=debug)
if rotors_name_save:
printd(rotors_name_save, rotors_number, debug=debug)
if rotors_name_save[0] == '/':
pass
elif rotors_name_save[0:2] == '..':
rotors_name_save = os.getcwd()[:os.getcwd().rfind('/')] + rotors_name_save[2:]
elif rotors_name_save[0:2] == './':
rotors_name_save = os.getcwd() + rotors_name_save[1:]
elif rotors_name_save[-7:] == '.rotors':
rotors_name_save = os.getcwd() + '/' + rotors_name_save
elif rotors_name_save:
rotors_name_save = options[0][:options[0].rfind('/')] + '/rotors/' + rotors_name_save
rotors = create_rotors(8, True, rotors_number)
if show: print_long('Rotor', rotors, min=min_print_length, max=max_print_length)
print('Rotors created:', rotors_name_save[rotors_name_save.rfind('/') + 1:], str(rotors_number) + " items")
save_file_all(rotors_name_save, rotors, 'rotors_in_one_file', show=True)
printd(rotors, debug=debug)
os.system('setterm -cursor on')
exit()