How to XOR a byte output?
Marko Rauhamaa
marko at pacujo.net
Wed Apr 13 10:27:29 EDT 2016
Chris Angelico <rosuav at gmail.com>:
> Let's just guess that you want to xor with the byte value 0xAA. We can
> do that fairly simply, using integer operations.
>
>>>> data = b'$//W?\xc0\x829\xa2\xb9\x13\x8c\xd5{\\'
>>>> bytes(b ^ 0xAA for b in data)
> b'\x8e\x85\x85\xfd\x95j(\x93\x08\x13\xb9&\x7f\xd1\xf6'
>
> Well, that doesn't look much more intelligible.
This looks clearer:
>>> code = b'a0\xed\xf0Z\x15]g^\xce3x'
>>> key = b')U\x81\x9c55*\x08,\xa2WY'
>>> bytes(c ^ k for c, k in zip(code, key)).decode()
'Hello world!'
Marko
More information about the Python-list
mailing list