简体   繁体   中英

How to XOR two hex strings so that each byte is XORed separately?

I have been posting similar questions here for a couple of days now, but it seems like I was not asking the right thing, so excuse me if I have exhausted you with my XOR questions :D.

To the point - I have two hex strings and I want to XOR these strings such that each byte is XORed separately (ie each pair of numbers is XORed separately). And I want to do this in python, and I want to be able to have strings of different lengths. I will do an example manually to illustrate my point (I used the code environment because it allows me to put in spaces where I want them to be):

Input:
s1 = "48656c6c6f"
s2 = "61736b"

Encoding in binary:
48 65 6c 6c 6f = 01001000 01100101 01101100 01101100 01101111
61 73 6b       = 01100001 01110011 01101011

XORing the strings:
01001000 01100101 01101100 01101100 01101111
                  01100001 01110011 01101011
                  00001101 00011111 00000100

Converting the result to hex:
00001101 00011111 00000100 = 0d 1f 04

Output:
0d1f04

So, to summarize, I want to be able to input two hex strings (these will usually be ASCII letters encoded in hex) of different or equal length, and get their XOR such that each byte is XORed separately.

Use binascii.unhexlify() to turn your hex strings to binary data, then XOR that, going back to hex with binascii.hexlify() :

>>> from binascii import unhexlify, hexlify
>>> s1 = "48656c6c6f"
>>> s2 = "61736b"
>>> hexlify(''.join(chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(unhexlify(s1[-len(s2):]), unhexlify(s2))))
'0d1f04'

The actual XOR is applied per byte of the decoded data (using ord() and chr() to go to and from integers).

Note that like in your example, I truncated s1 to be the same length as s2 (ignoring characters from the start of s1 ). You can encode all of s1 with a shorter key s2 by cycling the bytes:

>>> from itertools import cycle
>>> hexlify(''.join(chr(ord(c1) ^ ord(c2)) for c1, c2 in zip(unhexlify(s1), cycle(unhexlify(s2)))))
'2916070d1c'

You don't have to use unhexlify() , but it is a lot easier than looping over s1 and s2 2 characters at a time and using int(twocharacters, 16) to turn that into integer values for XOR operations.

The Python 3 version of the above is a little lighter; use bytes() instead of str.join() and you can drop the chr() and ord() calls as you get to iterate over integers directly:

>>> from binascii import unhexlify, hexlify
>>> s1 = "48656c6c6f"
>>> s2 = "61736b"
>>> hexlify(bytes(c1 ^ c2 for c1, c2 in zip(unhexlify(s1[-len(s2):]), unhexlify(s2)))) 
b'0d1f04'
>>> from itertools import cycle
>>> hexlify(bytes(c1 ^ c2 for c1, c2 in zip(unhexlify(s1), cycle(unhexlify(s2)))))
b'2916070d1c'

I found a very simple solution:

def xor_str(a,b):
    result = int(a, 16) ^ int(b, 16) # convert to integers and xor them
    return '{:x}'.format(result)     # convert back to hexadecimal

It will xor the string until one of theme ends

I'm not sure of what are you exactly searching for, but hopefully this be useful for you.

>>> def getstr(encoded):
     return "".join([chr(int(i+k, 16))for (i,k) in zip(encoded[0::2], encoded[1::2])])

>>> getstr(s1)
'Hello'

>>> getstr(s2)
'ask'

Starting with two normal strings, you can find your results doing something like this:

>>> "".join(reversed(["%02X" % (ord(c1) ^ ord(c2)) for c1, c2 in zip(reversed(getstr(s1)),       reversed(getstr(s2)))]))
'0D1F04'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM