-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
60 lines (45 loc) · 1.66 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
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 = b"\x00\x01\x02\x03\x04\x05\x06\x07"
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[:8])
key = AES.new(self.key, AES.MODE_CTR, counter=self.counter)
return key.decrypt(ciphertext[8:]).decode()
def _reset(self, nonce):
self.counter = Counter.new(nbits=64, prefix=nonce, initial_value=0, little_endian=False)
encryptor = Encryptor(KEY)
@app.route('/encrypt')
def encrypt():
plaintext = request.args.get("plaintext")
if not plaintext or len(plaintext) == 0:
return "Something wents wrong"
if "flag" in plaintext:
return "I don't like a word 'flag'."
return encryptor.encrypt(plaintext)
@app.route('/decrypt')
def decrypt():
ciphertext = request.args.get("ciphertext")
plaintext = encryptor.decrypt(ciphertext)
if plaintext == "gimme a flag":
# I dont gim it to you easily.
return encryptor.encrypt(FLAG)
else:
return plaintext
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, port=5000)