Previously doing a CTF challenge I found myself needing to XOR two byte strings in Python to reveal a key from the original text and 'ciphered' data (in this case by XOR).
I found an answer on stackoverflow showing how to XOR two strings to form a resulting string:
def sxor(s1,s2): return ''.join(chr(ord(a) ^ ord(b)) for a,b in zip(s1,s2))
Using this we could now do:
key = sxor('string 1', 'string 2')
But this only worked for strings and I had a byte string. To fix this I simply removed the
chr calls to only manipulate bytes.
def byte_xor(ba1, ba2): return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
So now I could do
key = byte_xor(b'string 1', b'string 2')