forked from AbdiHaryadi/classic-cryptology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
444 lines (357 loc) · 13.4 KB
/
app.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
from flask import *
from fileinput import filename
from werkzeug.utils import secure_filename
from io import BytesIO
from algorithm.hillCipher import HillCipher
from algorithm.playfair_cipher import PlayfairCipher
from algorithm.affine_cipher import AffineCipher
from algorithm.standard_vigenere_cipher import StandardVigenereCipher
from algorithm.auto_key_vigenere_cipher import AutoKeyVigenereCipher
from algorithm.extended_vigenere_cipher import ExtendedVigenereCipher
from algorithm.enigma_cipher import *
from utils.utils import *
from utils.output_formatter import OutputFormatter
import re
import os
app = Flask(__name__)
output_formatter = OutputFormatter(
no_space_display_value="option1",
five_letters_group_display_value="option2"
)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/hill/encrypt',methods = ['GET', 'POST'])
def hillCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key_size = int(input['text1'])
# isi key
key_input = input['text2']
key = formTextToArray(key_input, key_size)
# plaintext
plain = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Hill Cypher algorithm
hill = HillCipher(plain=plain, key=key)
print(hill.getKey())
# do cypher
hill.doEncryptAll()
cypher = ''.join(hill.getCypher())
print("res",input)
print("hasil", cypher)
print(hill.getKey())
result = output_formatter.format(cypher)
return render_template("hill.html", mode="Encrypt", plaintext=plain, result=result, n=key_size, array=key_input, display=display)
return render_template("hill.html", mode="Encrypt", display="option1")
@app.route('/hill/decrypt',methods = ['GET', 'POST'])
def hillDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key_size = int(input['text1'])
# isi key
key_input = input['text2']
key = formTextToArray(key_input,key_size)
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Hill Cypher algorithm
hill = HillCipher(cypher=cypher, key=key)
print(hill.getKey())
# do cypher
hill.doDecryptAll()
plain = ''.join(hill.getPlain())
print("res",input)
print("hasil", plain)
print(hill.getKey())
result = output_formatter.format(plain)
return render_template("hill.html", mode="Decrypt", ciphertext=cypher, result=result, n=key_size, array=key_input, display=display)
return render_template("hill.html", mode="Decrypt", display="option1")
@app.route('/playfair/encrypt',methods = ['GET', 'POST'])
def playfairCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
plain = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# playfair Cipher algorithm
playfair = PlayfairCipher(key=key)
# do cypher
cypher = playfair.encrypt(plain)
result = output_formatter.format(cypher)
return render_template("playfair.html", mode="Encrypt", plaintext=plain, result=result, key=key, display=display)
return render_template("playfair.html", mode="Encrypt", display="option1")
@app.route('/playfair/decrypt',methods = ['GET', 'POST'])
def playfairDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# playfair algorithm
playfair = PlayfairCipher(key=key)
# do decrypt
plain = playfair.decrypt(cypher)
result = output_formatter.format(plain)
return render_template("playfair.html", mode="Decrypt", ciphertext=cypher, result=result, key=key, display=display)
return render_template("playfair.html", mode="Decrypt", display="option1")
@app.route('/affine/encrypt',methods = ['GET', 'POST'])
def affineCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci skala
skala = int(input['text1'])
# ukuran kunci bias
bias = int(input['text2'])
# plaintext
plain = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Affine Cypher algorithm
affine = AffineCipher(scale_key=skala, bias_key=bias)
# do cypher
cypher = affine.encrypt(plain)
result = output_formatter.format(cypher)
return render_template("affine.html", mode="Encrypt", plaintext=plain, result=result, skala=skala, bias=bias, display=display)
return render_template("affine.html", mode="Encrypt", display="option1")
@app.route('/affine/decrypt',methods = ['GET', 'POST'])
def affineDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci skala
skala = int(input['text1'])
# ukuran kunci bias
bias = int(input['text2'])
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Affine Cypher algorithm
affine = AffineCipher(scale_key=skala, bias_key= bias)
# do decrypt
plain = affine.decrypt(cypher)
result = output_formatter.format(plain)
return render_template("affine.html", mode="Decrypt", ciphertext=cypher, result=result, skala=skala, bias=bias, display=display)
return render_template("affine.html", mode="Decrypt", display="option1")
@app.route('/standard/encrypt',methods = ['GET', 'POST'])
def standardVigenereCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
plain = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Standard Vigenere Cipher algorithm
standard = StandardVigenereCipher(key=key)
# do cypher
cypher = standard.encrypt(plain)
result = output_formatter.format(cypher)
return render_template("standard.html", mode="Encrypt", plaintext=plain, result=result, key=key, display=display)
return render_template("standard.html", mode="Encrypt", display="option1")
@app.route('/standard/decrypt',methods = ['GET', 'POST'])
def standardVigenereDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Standard Vigenere Cipher algorithm
standard = StandardVigenereCipher(key=key)
# do decrypt
plain = standard.decrypt(cypher)
result = output_formatter.format(plain)
return render_template("standard.html", mode="Decrypt", ciphertext=cypher, result=result, key=key, display=display)
return render_template("standard.html", mode="Decrypt", display="option1")
@app.route('/autokey/encrypt',methods = ['GET', 'POST'])
def autoKeyVigenereCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
plain = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Auto-Key Vigenere Cipher algorithm
autokey = AutoKeyVigenereCipher(key=key)
# do cypher
cypher = autokey.encrypt(plain)
result = output_formatter.format(cypher)
return render_template("autokey.html", mode="Encrypt", plaintext=plain, result=result, key=key, display=display)
return render_template("autokey.html", mode="Encrypt", display="option1")
@app.route('/autokey/decrypt',methods = ['GET', 'POST'])
def autoKeyVigenereDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Auto Key Vigenere Cipher algorithm
autokey = AutoKeyVigenereCipher(key=key)
# do decrypt
plain = autokey.decrypt(cypher)
result = output_formatter.format(plain)
return render_template("autokey.html", mode="Decrypt", ciphertext=cypher, result=result, key=key, display=display)
return render_template("autokey.html", mode="Decrypt", display="option1")
@app.route('/extended/encrypt',methods = ['GET', 'POST'])
def extendedVigenereCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
key_in_bytes = bytes(key, encoding="ascii")
# file handle
file = request.files["file"]
if file:
filename = secure_filename(file.filename)
encrypted_filename = filename + ".encrypted"
plain: bytes = file.read()
# Extended Vigenere Cipher algorithm
extended = ExtendedVigenereCipher(key=key_in_bytes)
# do cypher
cypher = extended.encrypt(plain)
return send_file(
BytesIO(cypher),
as_attachment=True,
download_name=encrypted_filename
)
else:
print("Warning: No files detected.")
return render_template("extended.html", mode="Encrypt", key=key)
return render_template("extended.html", mode="Encrypt", display="option1")
@app.route('/extended/decrypt',methods = ['GET', 'POST'])
def extendedVigenereDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1']
key_in_bytes = bytes(key, encoding="ascii")
# file handle
file = request.files["file"]
if file:
cypher: bytes = file.read()
filename = secure_filename(file.filename)
filename_match = re.match("(.*)\\.encrypted", filename)
if filename_match:
decrypted_filename = filename_match.group(1)
else:
decrypted_filename = filename + ".decrypted"
# Extended Vigenere Cipher algorithm
extended = ExtendedVigenereCipher(key=key_in_bytes)
# do decrypt
cypher = extended.decrypt(cypher)
return send_file(
BytesIO(cypher),
as_attachment=True,
download_name=decrypted_filename
)
else:
print("Warning: No files detected.")
result = output_formatter.format(plain)
return render_template("extended.html", mode="Decrypt", key=key)
return render_template("extended.html", mode="Decrypt", display="option1")
@app.route('/enigma/encrypt',methods = ['GET', 'POST'])
def enigmaCypher():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1'].upper()
# plaintext
plain = input['text3']
# display
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Enigma Cipher algorithm, , sementara ku Hill biar bisa jalan html nya :v
enigma = EnigmaCipher(
rotors=[
Rotor(letters="EKMFLGDQVZNTOWYHXUSPAIBRCJ", notch="Q"),
Rotor(letters="AJDKSIRUXBLHWTMCQGZNPYFVOE", notch="E"),
Rotor(letters="BDFHJLCPRTXVZNYEIWGAKMUSQO", notch="V")
],
reflector=Reflector(letters="YRUHQSLDPXNGOKMIEBFZCWVJAT"),
initial_positions=key
)
# do cypher
cypher = enigma.encrypt(plain)
result = output_formatter.format(cypher)
return render_template("enigma.html", mode="Encrypt", plaintext=plain, result=result, key=key, display=display)
return render_template("enigma.html", mode="Encrypt", display="option1")
@app.route('/enigma/decrypt',methods = ['GET', 'POST'])
def enigmaDecrypt():
if request.method == 'POST':
# other
input = request.form
# ukuran kunci
key = input['text1'].upper()
# plaintext
cypher = input['text3']
# display
display = input['inlineRadio']
output_formatter.set_display(display)
# Enigma
enigma = EnigmaCipher(
rotors=[
Rotor(letters="EKMFLGDQVZNTOWYHXUSPAIBRCJ", notch="Q"),
Rotor(letters="AJDKSIRUXBLHWTMCQGZNPYFVOE", notch="E"),
Rotor(letters="BDFHJLCPRTXVZNYEIWGAKMUSQO", notch="V")
],
reflector=Reflector(letters="YRUHQSLDPXNGOKMIEBFZCWVJAT"),
initial_positions=key
)
# do decrypt
plain = enigma.decrypt(cypher)
result = output_formatter.format(plain)
return render_template("enigma.html", mode="Decrypt", ciphertext=cypher, result=result, key=key, display=display)
return render_template("enigma.html", mode="Decrypt", display="option1")
# @app.route('/download')
# def download():
# uploads = os.path.join(current_app.root_path, DOWNLOAD_FOLDER)
# return send_from_directory(directory=uploads, filename=filename)
# @app.route('/download')
# def download_file():
# return send_file(DOWNLOAD_FOLDER, as_attachment=True)
if __name__ == '__main__':
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug = True)