I have a list of XOR encrypted bytes written in hex (contained in “cipher.txt”, one byte per line) and I know that the XOR key used is one byte. Therefore I’m trying a brute force approach like this:
f = open("cipher.txt", "r")
ciphers = f.readlines()
f.close
for x in range(0, 255):
key = bin(int(x))[2:]
for line in ciphers:
w = bin(int(line, 16))[2:]
y = int(w)
z = y ^ x
print chr(z),
print ""
There, my results (z) don’t stay binary. Instead I get results like “10010084”.
If I don’t use the “y = int(w)” part, I get “unsupported operand type(s) for ^: ‘str’ and ‘int'” as an error, even though I don’t understand why, since the definition of the variable is similar to the “key” variable which works.
I suspect that there’s one big really stupid mistake in my code which causes it to fail.
Can anyone advise?
Try this:
also you haven’t closed
f:I suggest using context manager in order to manage opened files: