xor function in python

20,148

Solution 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.

Solution 2

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:]

Solution 3

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))
Share:
20,148
Leo
Author by

Leo

Updated on April 08, 2020

Comments

  • Leo
    Leo about 4 years

    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?

    • Some programmer dude
      Some programmer dude over 11 years
      Can you please clarify, the "symbols" in the messages are in hex in text, i.e. strings like "46 6f 6f"?
    • Leo
      Leo over 11 years
      right, there it is -->> e877a5e68bea88d61b93ac5ee0d562e8e9...
    • Russell Borogove
      Russell Borogove over 11 years
      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?
  • Xavier
    Xavier over 11 years
    The best way to find out is to try, but python has arbitrary precision ints, so it should be ok.
  • yati sagade
    yati sagade over 11 years
    Won't this break if the two strings have unequal lengths?
  • Xavier
    Xavier over 11 years
    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')]
  • kaspersky
    kaspersky over 11 years
    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.
  • martineau
    martineau over 11 years
    I'd call ignoring part of one of the values "broken".
  • martineau
    martineau over 11 years
    I'd up-vote this if it included converting the result back to a string of hex digits...
  • Leo
    Leo over 11 years
    by the way, does it appropriate to strings with differents lengths? –
  • kaspersky
    kaspersky over 11 years
    @user1813163, yes, technically the shorter string (smaller number) will be prepending with leading zeroes.