Encrypt.py directly XORs the n-byte key with each n-byte chunk of the input
This means we can use known plaintext (the png header) to find the first 8 bytes of the secret key, and hopefully the key is shorter than 8 bytes. This is done by XORing the png header with the encrypted file (find_Key.py)
The key elephant was found, which can then be XORed with each chunk of 8 bytes of the ciphertext to decrypt the file (decrypt.py). Luckily, the key is the same length as the png header and this produces a valid png with the flag
# find_key.pyheader = b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a'with open("flag.png.enc", "rb") as f: enc = f.read(8)key = bytes(a ^ b for (a, b) in zip(header, enc))print(key)
# decrypt.pyKEY = b'elephant'with open("flag.png.enc", "rb") as f: data = bytearray(f.read())for i in range(len(data)): data[i] ^= KEY[i % len(KEY)]with open("flag.png", "wb") as g: g.write(data)
chall.py encrypts each byte in the input file individually. Using ECB means that the same character in the input will be encrypted to the same 16 byte block in the output
I go through the encrypted file, keeping a record of what ciphertext each plaintext character encrypts into
when we reach the secret, we hope that all the characters in the secret have been encountered already. we try to match each block in the output to our above record.