-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
56 lines (42 loc) · 1.58 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
from flask import Flask, request, render_template
from Crypto.Cipher import AES
from Crypto.Util import Counter
import os
FLAG = os.environ.get("FLAG") or "PLEASE_SET_A_FLAG"
KEY = os.environ.get("KEY") or os.urandom(32)
app = Flask(__name__)
class Encryptor(object):
def __init__(self, key):
self.key = key
def encrypt(self, plaintext):
nonce = os.urandom(15)
self._reset(nonce)
key = AES.new(self.key, AES.MODE_CTR, counter=self.counter)
return nonce.hex() + key.encrypt(plaintext.encode()).hex()
def decrypt(self, ciphertext):
ciphertext = bytes.fromhex(ciphertext)
self._reset(ciphertext[:15])
key = AES.new(self.key, AES.MODE_CTR, counter=self.counter)
return key.decrypt(ciphertext[15:]).decode()
def _reset(self, nonce):
self.counter = Counter.new(nbits=8, prefix=nonce, initial_value=0, little_endian=False, allow_wraparound=True)
encryptor = Encryptor(KEY)
@app.route('/encrypt', methods=['POST'])
def encrypt():
plaintext = request.form.get('plaintext')
if not plaintext or len(plaintext) == 0:
return "Something wents wrong"
return encryptor.encrypt(FLAG + plaintext)
@app.route('/decrypt', methods=['POST'])
def decrypt():
ciphertext = request.form.get('ciphertext')
plaintext = encryptor.decrypt(ciphertext)
if not plaintext.startswith(FLAG):
return "Something wents wrong"
else:
return plaintext[len(FLAG):]
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, port=5000)