# [pycrypto] string XOR

Paul_Koning at Dell.com Paul_Koning at Dell.com
Fri Oct 28 10:51:16 CST 2011

```It sure is.  I didn't remember that.  For that matter, Python 2.4 does this too, so that feature goes back a ways.  It also does that whenever you do arithmetic that exceeds the bounds of an int.  For example, "int("7fffffff",16) produces an "int" type, but do << 1 on it and you get a "long".  (You can tell by the L suffix when it is printed.)

paul

-----Original Message-----
From: pycrypto-bounces at lists.dlitz.net [mailto:pycrypto-bounces at lists.dlitz.net] On Behalf Of Dean Macinskas
Sent: Friday, October 28, 2011 12:42 PM
To: PyCrypto discussion list
Subject: Re: [pycrypto] string XOR

Paul,

Thanks.  It seems that in 2.7.2 the conversion is automatic: in other words, hex(int("ffffffffffffffff") == "ffffffffffffffff".

Dean

-----Original Message-----
From: pycrypto-bounces at lists.dlitz.net [mailto:pycrypto-bounces at lists.dlitz.net] On Behalf Of Paul_Koning at Dell.com
Sent: Friday, October 28, 2011 9:30 AM
To: pycrypto at lists.dlitz.net
Subject: Re: [pycrypto] string XOR

Python 2 has "int" and "long" types -- long is what crypto folks call "bignum".  Arithmetic operations work on both.  So if you want to do arithmetic on values too big for an int, put them into a long and do it then.

In Python 3, there is only "int" which is what used to be "long" (the small integer type went away) so the whole issue goes away.

paul

-----Original Message-----
From: pycrypto-bounces at lists.dlitz.net [mailto:pycrypto-bounces at lists.dlitz.net] On Behalf Of Dean Macinskas
Sent: Friday, October 28, 2011 8:58 AM
To: PyCrypto discussion list
Subject: Re: [pycrypto] string XOR

Dwayne,

I've been offline for a few days, so I apologize for the delay.  I'm a new pycrypto user, and missed this - I appreciate the heads-up.

I've got a followup question too: I need to do bit shifts on long (8 or 16 byte) strings.  I can't see any simple way to do this; the built-in Python bit shift functions only work on integers and don't seem to have multi-precision extensions.  Any advice on how to do this?

Thanks again,
Dean

-----Original Message-----
From: pycrypto-bounces at lists.dlitz.net [mailto:pycrypto-bounces at lists.dlitz.net] On Behalf Of Dwayne C. Litzenberger
Sent: Saturday, October 22, 2011 4:24 PM
To: PyCrypto discussion list
Subject: Re: [pycrypto] string XOR

On Fri, Oct 21, 2011 at 03:09:36PM +0100, Richard Moore wrote:
>On Fri, Oct 21, 2011 at 2:55 PM, Dean Macinskas
><dmacinskas at geobridge.net> wrote:
>>     ke = operator.xor(kbpkb,ve)
>>
>> TypeError: unsupported operand type(s) for ^: 'str' and 'str'
>>
>> Is there a way to do this in native Python (I'm using 2.7.2), or must
>> I use a library of some sort?
>
>Use ord() to convert each character to a number first, then chr() to
>convert the number back to a character.

Also, if you're using PyCrypto anyway, you can use its fast C implementation of a byte-wise string XOR:

from Crypto.Util.strxor import strxor

--
Dwayne C. Litzenberger <dlitz at dlitz.net>
OpenPGP: 19E1 1FE8 B3CF F273 ED17  4A24 928C EC13 39C2 5CF7 _______________________________________________
pycrypto mailing list
pycrypto at lists.dlitz.net
http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto
_______________________________________________
pycrypto mailing list
pycrypto at lists.dlitz.net
http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto
_______________________________________________
pycrypto mailing list
pycrypto at lists.dlitz.net
http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto
_______________________________________________
pycrypto mailing list
pycrypto at lists.dlitz.net
http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto
```