在下面的代码中,我试图解密一条加密消息,给定两条使用相同密钥(两次密码)加密的消息。该代码也可以按我的意愿工作,直到我尝试将十六进制字符串打印为 ascii 的最后一行。
我得到错误:
print result.decode('hex')
File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found
导致错误的十六进制字符串是:
ab51e67kba7<4:72fd`d
其中有一些非十六进制字符。我不确定为什么它里面有非十六进制。或者从这里去哪里。
这是完整的代码:
# Messages
m1 = "31aa4573aa487946aa15"
m2 = "32510ba9babebbbefd00"
# Key
k = "6b6bdfa4rqggrgwereff"
guess = 'aa'
#guess = guess.encode('hex')
result = ''
def strxor(a, b): # xor two strings of different lengths
if len(a) > len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
# Make cipher texts
c1 = strxor(m1,k)
c2 = strxor(m2,k)
# xor of the two messages
m1m2 = strxor(c1,c2)
# loop through each bit of the m1m2 message and xor against a test char or string
# see if any of the output makes sense
for e in range(0, len(m1), 2):
subString = m1m2[e:e+2]
try:
result = result + "".join( strxor(subString, guess))
except exception:
pass
#print hex and ascii results
print result
print result.decode('hex')