It turn out that in Python 3, attempting to write non-ASCII characters to a file without using ‘binary mode’ (a mode which deals with ’non-textual data’, hence the name of the challenge) has some less-than ideal results. Indeed, if you tried providing your own plain-text to
xor.py you might have noticed that there are somehow more bytes in the cipher-text after XORing that you started with in your plaintext. Ultimately, it appears that the
write.write(ciphertext) function is prepending either 0xc2 or 0xc3 to certain bytes. Simply adding a condition to exclude these, and providing the cipher-text as the input (since XOR is the inverse of itself) will provide a simple solution to this puzzle.
from random import seed, randrange seed(True, version=2) with open("plaintext.txt", 'r') as read, open("ciphertext.txt", 'w') as write: plaintext = read.read() for char in plaintext: A = ord(char) if A != 194 and A != 195: # exclude 0xc2 and 0xc3 B = randrange(256) ciphertext = chr(A ^ B) print(bytes([A ^ B])) write.write(ciphertext)