import base64
import os
from Crypto.Cipher import AES
from Crypto.Cipher.AES import MODE_CBC
from Crypto.Util.Padding import unpad


def decrypt(key: bytes, payload: bytes) -> bytes:
    aes = AES.new(key, mode=MODE_CBC, iv=payload[:16])
    return unpad(aes.decrypt(payload[16:]), 16)


def main():
    key = os.urandom(16)
    print("This is a very simple challenge. You just need to send message 'gib flag plox' and that's it.")
    print("Oh, by the way, the message has to be encrypted (and base64 encoded)")
    while True:
        payload = base64.b64decode(input("> "))
        try:
            decrypted = decrypt(key, payload)
            if b'gib flag plox' == decrypted:
                print(open('flag.txt', 'rb').read())
                return
            else:
                print("Nope")
        except:
            print(":(")


main()
