0

I am looking for a way that I can do bitwise operations (for crypto so mostly xor) on hex strings which could possibly be longer than I would want to fit in a Long. As an example I could have a lengthy hex literal I want to xor with other data. Below is an example of what I want to do and how I am doing it now but I believe at some limit the computer will hit it's limit in what it can put in Long so how can I do this more robustly?

#assuming lengths of messages are correct and key length as well
old_hex = "top secret ascii message".encode('hex')
encoded = "09e1c5f70a65ac56e55ac519458e7e53f36" #a literal hex encoded string
key_hex = int(old_hex,16) ^ int(encoded,16) #this currently works because this fits in Long
new_hex = "my new message to send 2".encode('hex')
ans_hex = key_hex ^ int(new_hex,16)

print 'old message in hex: 0x'+old_hex
print 'found a key in hex: '+str(hex(key_hex))
print 'new message in hex: 0x'+new_hex
print 'new message encode: '+str(hex(ans_hex))

So I will want to do XORs on longer strings that this and I am afraid they will not fit in a Long variable so how can I get around this? Do I have to break the string up into smaller chunks and do each part through the operation?

3
  • how long are your strings? python ints go on forever. 2 ** 4096 fits into an int Commented Jun 30, 2012 at 22:25
  • So if I paste in a paragraph or a whole page of hex you don't think it will hit the limit when converting to an int? Commented Jun 30, 2012 at 22:26
  • it shouldnt. if youre doing a simple xor cipher, just xor each character individually, if by some crazy method, you overflow a python int. otherwise, use ints as defined in the standard of the algorithm youre implementing Commented Jun 30, 2012 at 22:27

1 Answer 1

5

I believe at some limit the computer will hit it's limit in what it can put in Long

Yes, there is a limit but that limit is huge. Eventually you will run out of memory, but the same could happen with strings. Python's integer type will work fine.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, thanks I figured it would be similar to how C did things but I guess not. Good to know.

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.