-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
97 lines (75 loc) · 2.99 KB
/
app.py
File metadata and controls
97 lines (75 loc) · 2.99 KB
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
import base64
import io
from flask import Flask, render_template, request, jsonify
from Ciphers.CaesarCipher.CaesarCipher import CaesarCipher
app = Flask(__name__)
@app.route('/')
def index():
return render_template('base.html')
@app.route('/b64', methods=['POST'])
def b64():
if 'b64_plaintext_input' in request.form:
text = request.form['b64_plaintext_input']
ciphertext_input = False
elif 'b64_ciphertext_input' in request.form:
text = request.form['b64_ciphertext_input']
ciphertext_input = True
else:
return jsonify({'error': 'No input provided'})
result = {}
if ciphertext_input:
base64_bytes = text.encode("ascii")
sample_string_bytes = base64.b64decode(base64_bytes)
result['cleartext'] = str(sample_string_bytes, "ascii")
else:
sample_string_bytes = text.encode("ascii")
base64_bytes = base64.b64encode(sample_string_bytes)
result['ciphertext'] = str(base64_bytes.decode("ascii"))
return jsonify(result)
@app.route('/CaesarCipherED', methods=['POST'])
def CaesarCipherED():
if 'plaintext_input' in request.form:
text = request.form['plaintext_input']
ciphertext_input = False
elif 'ciphertext_input' in request.form:
text = request.form['ciphertext_input']
ciphertext_input = True
else:
return jsonify({'error': 'No input provided'})
key = int(request.form['key'])
result = {}
if ciphertext_input:
pad_file = request.files.get('pad_file')
if pad_file:
pad = io.StringIO(pad_file.stream.read().decode('utf-8'))
cipher = CaesarCipher(shift=key, use_pad=True, file=pad)
else:
cipher = CaesarCipher(shift=key, use_pad=False)
result['cleartext'] = cipher.decrypt(text)
if pad_file:
pad.seek(0)
new_cipher = CaesarCipher(shift=key, use_pad=True, file=pad)
encrypted_text = new_cipher.encrypt(result['cleartext'])
else:
new_cipher = CaesarCipher(shift=key, use_pad=False)
encrypted_text = new_cipher.encrypt(result['cleartext'])
result['ciphertext'] = encrypted_text
else:
pad_file = request.files.get('pad_file')
if pad_file:
pad = io.StringIO(pad_file.stream.read().decode('utf-8'))
cipher = CaesarCipher(shift=key, use_pad=True, file=pad)
else:
cipher = CaesarCipher(shift=key, use_pad=False)
result['ciphertext'] = cipher.encrypt(text)
if pad_file:
pad.seek(0)
new_cipher = CaesarCipher(shift=key, use_pad=True, file=pad)
decrypted_text = new_cipher.decrypt(result['ciphertext'])
else:
new_cipher = CaesarCipher(shift=key, use_pad=False)
decrypted_text = new_cipher.decrypt(result['ciphertext'])
result['cleartext'] = decrypted_text
return jsonify(result)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=3000, debug=True)