#!/usr/bin/env python

from binascii import hexlify, unhexlify

BLOCK = 6


def _xor(a, b):
    return bytes([i ^ j for i, j in zip(a, b)])


def get_key(command, tag):
    p_command = command
    while len(p_command) % 6 != 0:
        p_command += "0"

    blocks = [p_command[i:i+BLOCK] for i in range(0, len(p_command), BLOCK)]

    xored = b"\x00" * 6
    for b in blocks:
        xored = _xor(xored, b.encode())

    return _xor(unhexlify(tag), xored)


def cat_flag(key):
    print("cat flag.txt")
    tag = _xor(key, _xor("cat fl".encode(), "ag.txt".encode()))
    return hexlify(tag).decode()


command = input("Random command: ")
tag = input("Tag: ")

key = get_key(command, tag)
print(cat_flag(key))
