Decrypting hex string and getting non-hex characters when I try to print
In the code below I'm trying to decrypt an encrypted message given two
messages encrypted with the same key (two-time pad). The code works as I
want it too until the last line where I try an print out the hex string as
ascii.
I get the error:
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
The hex string that is causing the error is:
ab51e67kba7<4:72fd`d
Which has some non hex characters in it. I'm not sure why it has the
non-hex in it. Or where to go from here.
Here's the full code:
# 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')
No comments:
Post a Comment