In questa sfida hai la possibilità di cifrare un plaintext con una chiave key tramite la funzione
encrypt, che usa il cifario 3DES in maniera doppiamente sicura.
Interagisci con il server tramite il bottone SEND ed i rispettivi input field, oppure dalla console
tramite la funzione
await encrypt(plaintext, key)(si aspetta i parametri come stringhe).
Puoi usare lo HEX ENCODER/DECODER per convertire da hex ad
ascii e viceversa, oppure puoi usare dalla console le funzioni
hex2a(your_hex_input) e
a2hex(your_ascii_input).
import os
import json
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad
from flask import Flask, render_template
app = Flask(__name__)
FLAG = os.environ["FLAG"]
def isascii(char:str):
return 32 <= ord(char) < 127
@app.route("/encrypt/<plaintext>/<key>")
def get_encrypted_message(plaintext:str, key:str):
# validazione dell'input
if not all([isascii(char) for char in plaintext]):
return json.dumps({"error": "Plaintext must be valid ascii"})
try:
key = bytes.fromhex(key.removeprefix("0x"))
assert len(key) == 16 or len(key) == 24
if len(plaintext) % DES3.block_size != 0:
plaintext = pad(plaintext.encode(), DES3.block_size)
else:
plaintext = plaintext.encode()
except ValueError:
return json.dumps({"error": "Key must be valid hexadecimal"})
except AssertionError:
return json.dumps({"error": "Key must be 16 or 24 bytes long"})
# creazione del cifrario con la chiave data
try:
cipher = DES3.new(key, DES3.MODE_ECB)
except ValueError:
return json.dumps({"error": "Key 1 and Key 2 must be different"})
# cifratura del plaintext dato
ciphertext = cipher.encrypt(cipher.encrypt(plaintext)).hex()
# controllo della condizione per superare la sfida
if ciphertext == "44616d6d69206c6120666c6167212121":
return json.dumps({"result": "success", "flag": FLAG})
else:
return json.dumps({"result": "failure", "ciphertext": ciphertext})