import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

flag = os.environ.get('FLAG', '<REDACTED>')

KEY = os.urandom(32)

def encrypt(data):
    cipher = AES.new(KEY, AES.MODE_ECB)
    return cipher.encrypt(pad(data, 16))

def decrypt(data):
    cipher = AES.new(KEY, AES.MODE_ECB)
    return unpad(cipher.decrypt(data), 16)


print(r'''
   ____                        ____
  / ___| __ _ _ __ ___   ___  / ___|  __ ___   _____ _ __
 | |  _ / _` | '_ ` _ \ / _ \ \___ \ / _` \ \ / / _ \ '__|
 | |_| | (_| | | | | | |  __/  ___) | (_| |\ V /  __/ |
  \____|\__,_|_| |_| |_|\___| |____/ \__,_| \_/ \___|_|

''')

while True:
    action = input('action = ')

    if action == 'save':
        name = input('name = ').replace(';', '')
        state = f'name={name};level=1'.encode()
        print('state =', encrypt(state).hex())
        continue

    if action == 'load':
        state_enc = bytes.fromhex(input('state = '))
        state = decrypt(state_enc).decode()

        name = state.split(';')[0].split('=', 1)[1]
        level = state.split(';')[1].split('=', 1)[1]

        if level == '1337':
            print(f'Well done, {name}! You cleared all levels.')
            print(f'Proof of your win: {flag}')
            break

        print(f'Player {name}, ready? Resuming at Level {level}!')
        continue
