How to XOR a byte output?
Peter Otten
__peter__ at web.de
Thu Apr 14 05:16:53 EDT 2016
durgadevi1 wrote:
>
>>
>> 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
>
> Hi, I have gotten another error message when working with the bytes(c ^ k
> for c, k in zip(code, key)).decode().
>
> Here is the error.
> print(bytes(c ^ k for c, k in zip(CODE, key)).decode())
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
> invalid start byte
>
> What I did is XOR the CODE with a certain value before using the
>
> bytes(c ^ k for c, k in zip(CODE, key)).decode() code.
>
> However, I get no errors when using values 0 to 127 to XOR with CODE. But
> I get errors when using values (128 to 255). May I know how I can modify
> the program code so that i can XOR with values (128 to 255)?
By default bytes.decode() interprets the sequence of bytes as UTF-8. This
will fail if for example there is a lone 0x85 because no encoded character
in UTF-8 starts with that byte:
>>> code = b'a0\xed\xf0Z\x15]g^\xce3x'
>>> key = b'\xe4U\x81\x9c55*\x08,\xa2WY'
>>> decrypted = bytes(c^k for c, k in zip(code, key))
>>> decrypted
b'\x85ello world!'
>>> decrypted.decode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x85 in position 0:
invalid start byte
If you explicitly choose an encoding that maps every single byte to exactly
one character (e. g. ISO-8859-15),
>>> decrypted.decode("iso-8859-15")
'\x85ello world!'
the conversion cannot fail -- but the result may still not make sense.
More information about the Python-list
mailing list