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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With