Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #106986

Re: How to XOR a byte output?

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: How to XOR a byte output?
Date Thu, 14 Apr 2016 11:16:53 +0200
Organization None
Lines 54
Message-ID <mailman.99.1460625428.15650.python-list@python.org> (permalink)
References <387506b1-b645-4907-a45c-81a8c3043099@googlegroups.com> <CAPTjJmpuG=ew7FPwhdtsgDyjDc8d_T8-zbWuVxZy_sUUGj+R0Q@mail.gmail.com> <mailman.71.1460554158.15650.python-list@python.org> <874mb5twha.fsf@elektro.pacujo.net> <2566ab7f-12bd-4f3b-92b2-45104b8b7dd8@googlegroups.com> <nenn66$np6$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 16/UJ6lOl/PyuV2QP10o+waQKwE43hcTKzMLrYAygdxg==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'encoded': 0.05; 'utf-8': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'xor': 0.09; 'encoding': 0.15; 'explicitly': 0.15; "'hello": 0.16; '127': 0.16; 'codec': 0.16; 'decode': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'bytes': 0.18; '>>>': 0.20; '"",': 0.22; 'code.': 0.23; 'errors': 0.23; '(most': 0.24; 'skip:b 30': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X-Complaints-To:1': 0.26; 'error': 0.27; 'sequence': 0.27; 'values': 0.28; 'looks': 0.29; 'character': 0.29; 'starts': 0.29; 'code': 0.30; 'error.': 0.31; 'certain': 0.31; 'another': 0.32; "can't": 0.32; 'traceback': 0.33; 'file': 0.34; 'fail': 0.35; 'maps': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'default': 0.61; 'here': 0.66; 'choose': 0.68; 'gotten': 0.76; 'lone': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd859e.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nenn66$np6$1@ger.gmane.org>
X-Mailman-Original-References <387506b1-b645-4907-a45c-81a8c3043099@googlegroups.com> <CAPTjJmpuG=ew7FPwhdtsgDyjDc8d_T8-zbWuVxZy_sUUGj+R0Q@mail.gmail.com> <mailman.71.1460554158.15650.python-list@python.org> <874mb5twha.fsf@elektro.pacujo.net> <2566ab7f-12bd-4f3b-92b2-45104b8b7dd8@googlegroups.com>
Xref csiph.com comp.lang.python:106986

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to XOR a byte output? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-13 06:18 -0700
  Re: How to XOR a byte output? Chris Angelico <rosuav@gmail.com> - 2016-04-13 23:29 +1000
    Re: How to XOR a byte output? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-13 06:51 -0700
      Re: How to XOR a byte output? Chris Angelico <rosuav@gmail.com> - 2016-04-14 00:31 +1000
      Re: How to XOR a byte output? Stephen Hansen <me+python@ixokai.io> - 2016-04-13 08:33 -0700
    Re: How to XOR a byte output? Marko Rauhamaa <marko@pacujo.net> - 2016-04-13 17:27 +0300
      Re: How to XOR a byte output? Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-13 09:30 -0600
      Re: How to XOR a byte output? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-14 01:49 -0700
        Re: How to XOR a byte output? Peter Otten <__peter__@web.de> - 2016-04-14 11:16 +0200
        Re: How to XOR a byte output? Marko Rauhamaa <marko@pacujo.net> - 2016-04-14 12:18 +0300
          Re: How to XOR a byte output? Chris Juried <cjuried@yahoo.com> - 2016-04-14 23:05 +0000
          Re: How to XOR a byte output? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-04-15 08:06 -0400
  Re: How to XOR a byte output? alister <alister.ware@ntlworld.com> - 2016-04-13 16:15 +0000
    [OT] A doubt about a doubt, was Re: How to XOR a byte output? Peter Otten <__peter__@web.de> - 2016-04-13 18:59 +0200
      Re: [OT] A doubt about a doubt, was Re: How to XOR a byte output? Rustom Mody <rustompmody@gmail.com> - 2016-04-13 18:54 -0700
        Re: [OT] A doubt about a doubt Peter Otten <__peter__@web.de> - 2016-04-14 14:26 +0200
  Re: How to XOR a byte output? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-14 01:32 -0700
  Re: How to XOR a byte output? durgadevi1 <srirajarajeswaridevikrupa@gmail.com> - 2016-04-15 04:46 -0700

csiph-web