0

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

2
  • Are cipher_text1 and cipher_text2 the plain text or binary? Commented Feb 14, 2022 at 2:59
  • It may be useful to you: stackoverflow.com/questions/29408173/… Commented Feb 14, 2022 at 3:05

2 Answers 2

0

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.