If I have:
cipher_text1 = "3b101c091d53320c000910"
cipher_text2 = "071d154502010a04000419"
How do I XOR the two cipher texts using Python to get:
cipher_text1_XOR_cipher_text2 = "3c0d094c1f523808000d09"
Thanks
If I have:
cipher_text1 = "3b101c091d53320c000910"
cipher_text2 = "071d154502010a04000419"
How do I XOR the two cipher texts using Python to get:
cipher_text1_XOR_cipher_text2 = "3c0d094c1f523808000d09"
Thanks
Try this out:
cipher_text1 = "3b101c091d53320c000910"
cipher_text2 = "071d154502010a04000419"
def xor_ciphers(cipher1, cipher2):
bin1 = int(format(int(cipher1, 16), f'0{str(len(cipher1))}b'), 2)
bin2 = int(format(int(cipher2, 16), f'0{str(len(cipher2))}b'), 2)
return hex(bin1 ^ bin2)
print(xor_ciphers(cipher_text1, cipher_text2))
Output:
0x3c0d094c1f523808000d09
Start with the int()
function, which allows you to specify a base in which to interpret a string to be converted to a numeric value. The default base the function assumes is 10, but 16 works too and is what's appropriate for the strings you have. So you convert your two strings to numeric values with int()
, and then perform the XOR on those values. Then, you apply the hex()
function to the result convert it to a hex string. Finally, since you asked for a result without Ox
at the front, you apply the appropriate slice to chop off the first two characters of what hex()
returns:
cipher_text1 = "3b101c091d53320c000910"
cipher_text2 = "071d154502010a04000419"
cipher_text1_XOR_cipher_text2 = hex(int(cipher_text1, 16) ^ int(cipher_text2, 16))[2:]
print(cipher_text1_XOR_cipher_text2)
Result:
3c0d094c1f523808000d09