I am trying to solve the following puzzle from CryptoHack involving two images encrypted with the same key:
I've hidden two cool images by XOR with the same secret key so you can't see them!
If I compare both images using an online image comparison tool I can see traces of a new image containing what seems to be the solution to the puzzle. However, I don't think this is the intended way to solve this puzzle.
So I tried to write a program to XOR the two encrypted images together, because the XORed keys should cancel out that way. I XORed both files byte by byte and saved the result as a PNG file. But this new PNG won't even open - my image viewer says it's a faulty file.
This is my Python code:
from binascii import unhexlify with open("lemur.png", mode='rb') as fl: lemur = fl.read() with open("flag.png", mode='rb') as ff: flag = ff.read() d = b'' for b1, b2 in zip(lemur, flag): d += bytes([b1^b2]) with open("new.png", mode='wb') as fn: fn.write(d)
One thing I noticed is that the lemur.png file is one byte longer than the other one. The above program stops XOR at the length of the smaller file. I also tried appending the last byte of the bigger file to the end. But even that doesn't help.
Is my approach incorrect, and if not, what is the right way to compare the two images?