3

Running in IDLE, Python 3.6.5 and Python 2.7.15 I have a strange problem with XOR. I get the correct answer with Python 2.7, and rubbish with Python 3.6. The Python 3.6 and 2.7 are not agreeing on a simple XOR. This is not an IDLE issue, since the behavior is the same in cygwin.

>>> ciphertext

'466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'

Python 2.7

>>> ciphertext.decode('hex')
'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
>>> for x, y in zip(ciphertext.decode('hex'), ' '*10):
    print "ord(x): " + chr(ord(x))
    print "ord(y): " + chr(ord(y))
    print(chr(ord(x) ^ ord(y)))


ord(x): F
ord(y):  
f
ord(x): m
ord(y):  
M
ord(x): 
ord(y): 
&
ord(x): ì
ord(y):  
Ì
ord(x): é
ord(y):  
É
ord(x): ˜
ord(y):  
¸
ord(x): ·
ord(y):  
—
ord(x): ¢
ord(y):  
‚
ord(x): û
ord(y):  
Û
ord(x): 
ord(y):  
=

Python 3.6

 >>> bytes.fromhex(ciphertext)

   b'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
>>> for x, y in zip(bytes.fromhex(ciphertext), ' '*10):
    print("x: ", chr(x))
    print("ord(y): ", chr(ord(y)))
    print(chr(x^ord(y)))


x:  F
ord(y):   
f
x:  m
ord(y):   
M
x:  
ord(y):   
&
x:  ì
ord(y):   
Ì
x:  é
ord(y):   
É
x:  
ord(y):   
¸
x:  ·
ord(y):   
<- different value
x:  ¢
ord(y):   
<- different value
x:  û
ord(y):   
Û
x:  
ord(y):   
=

In a cygwin window I get the following:
$ ./python2_test.py
Fm▒阷▒▒FO▒,▒vAݪ<▒▒A▒
f▒▒Y▒R▒▒▒*▒▒▒9▒ϯ▒▒▒▒▒▒▒2P;▒g▒:▒▒W\;▒ܛ▒▒7SA▒▒<▒O▒
ord(x): F
ord(y):
f
ord(x): m
ord(y):
M
ord(x):
ord(y):
&
ord(x): ▒
ord(y):
▒
ord(x): ▒
ord(y):
▒
ord(x): ▒
ord(y):
▒
ord(x): ▒
ord(y):
▒
ord(x): ▒
ord(y):
▒
ord(x): ▒
ord(y):
▒
ord(x):
ord(y):
=
$ ./python3_test.py
b'Fm\x06\xec\xe9\x98\xb7\xa2\xfb\x1dFO\xed,\xedvA\xdd\xaa<\xc3\x1c\x99A\xcf\x11\n\xbb\xf4\t\xed9Y\x80\x05\xb39\x9c\xcf\xaf\xb6\x1d\x03\x15\xfc\xa0\xa3\x14\xbe\x13\x8a\x9f2P;\xed\xac\x80g\xf0:\xdb\xf3W\\;\x8e\xdc\x9b\xa7\xf57S\x05A\xab\x0f\x9f<\xd0O\xf5\rf\xf1\xd5Y\xbaR\x0e\x89\xa2\xcb*\x83'
x:  F
ord(y):
f
x:  m
ord(y):
M
x:
ord(y):
&
x:  ì
ord(y):
Ì
x:  é
ord(y):
É
x:  
ord(y):
¸
x:  ·
ord(y):
<- missing
x:  ¢
ord(y):
<- missing
x:  û
ord(y):
Û
x:
ord(y):
=

Any help in sorting this out, would be greatly appreciated. Cheers.

1 Answer 1

1

I think you're seeing a problem of encoding. If you try to reproduce the steps printing numbers instead of chars, you'll see no difference. These are the scripts:

Python2:

ciphertext = '466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'

out1 = []
out2 = []
out3 = []
for x, y in zip(ciphertext.decode('hex'), ' '*10):
    out1.append(hex(ord(x)))
    out2.append(hex(ord(y)))
    out3.append(hex(ord(x) ^ ord(y)))
print out1
print out2
print out3

Python 3:

ciphertext = '466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83'
out1 = []
out2 = []
out3 = []
for x, y in zip(bytes.fromhex(ciphertext), ' '*10):
    out1.append(x)
    out2.append(ord(y))
    out3.append(x ^ ord(y))
print(out1)
print(out2)
print(out3)

If you execute them both, you'll see that the output is the same.

You can see it directly on ideone, python2 and python3

EDIT: The execution of the script that I have given, slightly modified to show hex instead of raw numbers, give me as an output:

Python2:

['0x46', '0x6d', '0x6', '0xec', '0xe9', '0x98', '0xb7', '0xa2', '0xfb', '0x1d']
['0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20']
['0x66', '0x4d', '0x26', '0xcc', '0xc9', '0xb8', '0x97', '0x82', '0xdb', '0x3d']

Python3:

['0x46', '0x6d', '0x6', '0xec', '0xe9', '0x98', '0xb7', '0xa2', '0xfb', '0x1d']
['0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20', '0x20']
['0x66', '0x4d', '0x26', '0xcc', '0xc9', '0xb8', '0x97', '0x82', '0xdb', '0x3d']

With the same input data you gave, I have a different input but my output data are coherent between Python2 and Python3.

Sign up to request clarification or add additional context in comments.

6 Comments

bracco23 - thanks for your prompt reply, but I am getting differences between the python 3 and python 2 xor's in my program.
bracco23 - thanks for your prompt reply, but I am getting differences between the python 3 and python 2 XOR's in my program. As an example: in python 2 I get the following: [66,c9,cd,29,19,19,80,12,d0,fc,8b,bf,f0,9a], which is correct,while in the python 3 XOR with the same data I get [66,c389,c38d,29,19,19,c280,12,c390,c3bc,c28b,c2bf,c3b0,c29a] which causes errors. I have no idea where hex values like 'c389' can come from in my data.
@JamOne I have edited the answer giving my output, which is the same between python2 and python3. If you can, edit your question and use a service like ideone to share an executable minimal reproducible example (don't forget to include the code in the question itself.)
once again many thanks for your patience. I have used the ideone service.
once again many thanks for your patience. I have used the ideone service. For the python 2.6 ideone.com/eIRNh8 for the python 3.4 ideone.com/0RLKUN essentially the very same code, with the minor differences between python 2 and 3, but I get very difference answers. For the 2.6 version, the first 10 bytes [66 4d 26 cc c9 b8 97 82 db 3d]. For the 3.4 version, the first 10 bytes [66 4d 26 c38c c389 c2b8 c297 c282 c39b 3d]. I simply cannot figure out why the difference, and it gives the wrong results in python 3.x
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.