1

I'll be happy if someone can help with XOR function in python.

for example, I have two quite big messages (about 300 symbols) that is writteb by hex code how can I XOR them properly? I tried use general functions and converted to another types, but I wasn't able to do that(

I don't know which type of data I need to convert for?

3
  • Can you please clarify, the "symbols" in the messages are in hex in text, i.e. strings like "46 6f 6f"? Commented Nov 10, 2012 at 18:01
  • right, there it is -->> e877a5e68bea88d61b93ac5ee0d562e8e9... Commented Nov 10, 2012 at 18:04
  • 3
    1. It looks like you're trying to do crypto. Use an existing crypto library instead of rolling your own. 2. What are your inputs and desired outputs? What have you tried? Commented Nov 10, 2012 at 18:08

3 Answers 3

6

If I get your question correctly, if you have 2 hex numbers represented as string:

a = "e877a5e68bea88d61b93ac5ee0d562e8e9"
b = "23fe3231699ade23482"

you can xor any of them with some mask, by converting to int, and applying bitwise xor operator:

xor_result = int(a, 16) ^ int(b, 16)

print '%x' % xor_result

and, if you want to keep the original format

string_xor_result = hex(xor_result)[2:]
Sign up to request clarification or add additional context in comments.

5 Comments

The best way to find out is to try, but python has arbitrary precision ints, so it should be ok.
As Xavier said, python can handle very, very large integers (the only limit comes from the machine available memory), so a 300 digits hex number (~16 ** 300) isn't a problem.
I'd up-vote this if it included converting the result back to a string of hex digits...
by the way, does it appropriate to strings with differents lengths? –
@user1813163, yes, technically the shorter string (smaller number) will be prepending with leading zeroes.
1

Iterate over the string chars, then convert each char to int (int(a,16)), then apply xor, reconvert to hex with hex, strip the leading '0x' with [2:], and finally join everything

stra = 'abc'
strb = 'abd'
''.join(hex( int(a,16) ^ int(b,16) )[2:] for a,b in zip(stra, strb))

Note that, as pointed in the comments, it will work only if the two strings have the same length. Otherwise some chars of the longer string will be ignored.

3 Comments

Won't this break if the two strings have unequal lengths?
It depends on what you call break :). zip will return tuples only for the minimum length of its parameters. zip('abc','ab') is [('a', 'a'), ('b', 'b')]
I'd call ignoring part of one of the values "broken".
1

If two strings do not have the same length you can add one line, cycle()

stra = raw_input("msg: ")
strb = raw_input("key: ")
strb = cycle(strb)

print ''.join(hex( int(a,16) ^ int(b,16) )[2:] for a,b in zip(stra, strb))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.