How to XOR a byte output?

Ian Kelly ian.g.kelly at gmail.com
Wed Apr 13 11:30:48 EDT 2016


On Wed, Apr 13, 2016 at 8:27 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
> 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!'

But that's not the code from the OP's post. The solution is obviously this:

>>> code = b'$//W?\xc0\x829\xa2\xb9\x13\x8c\xd5{'
>>> key = b'm\x0fC8I\xa5\xa2i\xdb\xcd{\xe3\xbbZ'
>>> bytes(c ^ k for c, k in zip(code, key)).decode()
'I love Python!'



More information about the Python-list mailing list