Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: XOR hex strings [duplicate]

Tags:

Possible Duplicate:
bitwise XOR of hex numbers in python

I am trying to XOR two hex strings in Python and did not really know where to start from.

I have two hex strings:

a = "32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb" b = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f90" 

Should I be using this ?

  1. return "".join([chr((x) ^ (y)) for (x,y) in zip(a[:len(b)], b)])
  2. return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])

I don't understand the difference with the two codes above. Why chr and ord? I have also seen people using int(hex,16).

like image 575
Kok Leong Fong Avatar asked Jan 25 '13 16:01

Kok Leong Fong


People also ask

How do you get XOR of two strings?

Approach: The idea is to iterate over both the string character by character and if the character mismatched then add “1” as the character in the answer string otherwise add “0” to the answer string to generate the XOR string.

How do you XOR two strings in python?

Use the ^ Operator to Perform the Bitwise Exclusive OR of Two Strings in Python. You can use the ^ operator to perform Bitwise XOR strings in Python.

How does XOR work with hexadecimal?

The XOR is commutative so it starts with the first two hexadecimal numbers, XORs them together, and gets the result. Then it XORs the result with the third hexadecimal and gets the next result.


1 Answers

You are missing a couple of things here.

First, you will not want to XOR those strings. You have the strings in an encoded form, therefore, you need to .decode() them first:

binary_a = a.decode("hex") binary_b = b.decode("hex") 

Then, as already mentioned, the zip() function stops iterating as soon as one of the two sequences is exhausted. No slicing is needed.

You need the second version of the loop: First, you want to get the ASCII value of the characters: ord() produces a number. This is necessary because ^ only works on numbers.

After XORing the numbers, you then convert the number back into a character with chr:

def xor_strings(xs, ys):     return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(xs, ys))  xored = xor_strings(binary_a, binary_b).encode("hex") 

Using .encode() at the end, we get the binary string back into a form, that prints nicely.

like image 114
phant0m Avatar answered Sep 21 '22 02:09

phant0m