在IDLE,Python 3.6.5和Python 2.7.15中运行我对XOR有一个奇怪的问题。使用python 2.7可以得到正确的答案,使用python 3.6可以得到正确的答案。 Python 3.6和2.7在简单的XOR上不一致。这不是空闲问题,因为在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):
=
在解决此问题方面的任何帮助,将不胜感激。 干杯。
答案 0 :(得分:1)
我认为您遇到编码问题。如果您尝试重现打印数字而不是字符的步骤,则不会有任何区别。这些是脚本:
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)
如果同时执行它们,您将看到输出是相同的。
您可以直接在ideone python2和python3上看到它
编辑:我给出的脚本的执行经过稍微修改以显示十六进制而不是原始数字,将其作为输出:
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']
使用您提供的相同输入数据,我有不同的输入,但是我的输出数据在Python2和Python3之间是一致的。