-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.py
executable file
·294 lines (272 loc) · 8.93 KB
/
cmd.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
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
from util import *
from md5 import *
from cramer import encrypt
from cramer import encryptFile
from cramer import decrypt
from cramer import decryptFile
from signal import signal
from signal import SIGINT
from fish import cipher_threefish_msg
from fish import cipher_threefish_file
from fish import decipher_threefish_msg
from fish import decipher_threefish_file
from platform import system
import keyboard
import os
import sys
# definition of constants
dictMenu = {
"Chiffrement Symétrique ThreeFish" : [
{
"Chiffrement D'un Message" : [
{
"ECB" : ["Saisir Votre Message:"]
},{
"CBC" : ["Saisir Votre Message:"]
}
]
},{
"Chiffrement D'un Fichier" : [
{
"ECB" : ["Saisir Le Nom Du Fichier:"]
},{
"CBC" : ["Saisir Le Nom Du Fichier:"]
}
]
}
],
"Chiffrement De Cramer-Shoup" : [
{
"Chiffrement D'un Message" : ["Saisir Votre Message:"]
},{
"Chiffrement D'un Fichier" : ["Saisir Le Nom Du Fichier:"]
}
],
"Hashage" : [
{
"Hashage D'un Message" : ["Saisir Votre Message:"]
},{
"Hashage D'un Fichier" : ["Saisir Le Nom Du Fichier:"]
}
],
"Déciffrement Symétrique ThreeFish" : [
{
"Déchiffrement D'un Message" : [
{
"ECB" : ["Saisir Votre Message:"]
},{
"CBC" : ["Saisir Votre Message:"]
}
]
},{
"Déchiffrement D'un Fichier" : [
{
"ECB" : ["Saisir Votre Message:"]
},{
"CBC" : ["Saisir Votre Message:"]
}
]
}
],
"Déchiffrement De Cramer-Shoup" : [
{
"Déchiffrement D'un Message" : ["Saisir Votre Message:"]
},{
"Déchiffrement D'un Fichier" : ["Saisir Le Nom Du Fichier:"]
}
],
"Vérification D'un Hash" : [
"Saisir Votre Hashage:",
"Saisir Votre Message:"
]
}
S = magenta("""
\x1b[2J
Three Fish && Cramer Shoup - Cheng JIANG && Shunjie TU\n
""")
E = magenta("""
Press 'up' and 'down' to choose\nPress 'left' to back and 'right' to next
Press ESC to exit
""")
M = list(dictMenu) # F menu
m = [] # store menus except the F menu,
n = [] # store idx which has been chosen by user
idx = 0 # store idx for now, which sign that '=> ' should be at which line
menu = [dictMenu] # store complete menu
rs = [] # rs answers of user
L = lambda arr: (arr[len(arr) - 1])
F = lambda arr: (arr[0])
def cls(arr):
while len(arr) > 0:
arr.pop()
def ask(arr):
n = len(arr)
while len(arr) > 0:
if len(arr) == n:
rs.append(input('\n-> ' + arr.pop() + ' '))
else:
rs.append(input('-> ' + arr.pop() + ' '))
# print menu which is m[len(m) - 1] or M
def show():
s = S
if len(m) == 0:
for i in M:
if M.index(i) == idx:
s += green('=> ') + cyan(i) + '\n'
else:
s += green(' ') + cyan(i) + '\n'
else:
for i in m[len(m) - 1]:
if m[len(m) - 1].index(i) == idx:
s += green('=> ') + cyan(i) + '\n'
else:
s += green(' ') + cyan(i) + '\n'
s += E
print(s)
def init():
global menu
if F(n) == 5:
ask(L(menu)[M[n[0]]])
if verify(F(rs), L(rs)):
print(green("\n\tOk!"))
else:
print(red("\n\tError!"))
os._exit(0)
elif len(n) == 1 and len(m) == 0:
menu.append(L(menu)[M[n[0]]])
m.append(dechunk([list(i) for i in L(menu)]))
elif len(n) > len(m):
t = L(m)[L(n)]
for i in L(menu):
if F(list(i)) == t:
menu.append(i[t])
m.append(dechunk([list(j) for j in L(menu)]))
if F(n) in [1,2,4] and len(n) == 2:
rs.append(n[1])
ask([L(L(menu))])
if F(n) == 2:
if F(rs) == 0:
print(green(md5(L(rs))))
elif F(rs) == 1:
try:
md5fic(L(rs))
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
elif F(n) == 1:
if F(rs) == 0:
print(green(encrypt(L(rs))))
elif F(rs) == 1:
try:
encryptFile(L(rs))
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
elif F(n) == 4:
if F(rs) == 0:
print(green(decrypt(L(rs))))
elif F(rs) == 1:
try:
decryptFile(L(rs))
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
os._exit(0)
elif F(n) in [0,3] and len(n) == 3:
rs.append(n[1])
rs.append(n[2])
ask([L(L(menu))])
if F(n) == 0:
MSG_FILE, ECB_CBC, s = rs
if MSG_FILE == 0:
if ECB_CBC == 0:
print(green(cipher_threefish_msg(s, cts.MODE_ECB)))
elif ECB_CBC == 1:
print(green(cipher_threefish_msg(s, cts.MODE_CBC)))
elif MSG_FILE == 1:
if ECB_CBC == 0:
try:
cipher_threefish_file(s, cts.MODE_ECB)
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
elif ECB_CBC == 1:
try:
cipher_threefish_file(s, cts.MODE_CBC)
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
elif F(n) == 3:
MSG_FILE, ECB_CBC, s = rs
if MSG_FILE == 0:
if ECB_CBC == 0:
print(green(decipher_threefish_msg(s, cts.MODE_ECB)))
elif ECB_CBC == 1:
print(green(decipher_threefish_msg(s, cts.MODE_CBC)))
elif MSG_FILE == 1:
if ECB_CBC == 0:
try:
decipher_threefish_file(s, cts.MODE_ECB)
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
elif ECB_CBC == 1:
try:
decipher_threefish_file(s, cts.MODE_CBC)
print(green("Ok!"))
except FileNotFoundError:
print(magenta("No Such File!"))
os._exit(0)
# listen left
def onLeft():
global idx
try:
m.pop()
n.pop()
menu.pop()
cls(rs)
idx = 0
init()
show()
except IndexError:
show()
# listen right
def onRight():
global idx
n.append(idx)
idx = 0
init()
show()
# listen up
def onUp():
global idx
if idx - 1 < 0:
idx = (len(M) - 1) if len(n) == 0 else (len(m[len(m) - 1]) - 1)
else:
idx = idx - 1
show()
# listen down
def onDown():
global idx
idx = ((idx + 1) % len(M)) if len(n) == 0 else ((idx + 1) % len(m[len(m) - 1]))
show()
# print start menu
show()
if system() == 'Windows':
keyboard.add_hotkey(72, onUp) # up clicked
keyboard.add_hotkey(80, onDown) # down clicked
keyboard.add_hotkey(75, onLeft) # left clicked
keyboard.add_hotkey(77, onRight) # right clicked
else:
keyboard.add_hotkey(103, onUp) # up clicked
keyboard.add_hotkey(108, onDown) # down clicked
keyboard.add_hotkey(105, onLeft) # left clicked
keyboard.add_hotkey(106, onRight) # right clicked
def handler(signal, frame):
print(green("\n\tBye!"))
sys.exit(0)
signal(SIGINT, handler)
# if 'esc' clicked, exit programme
keyboard.wait('esc')