XOR Binascii Hex Strings using the PyCrypto module

Y

yaipa

I snipped this bit of code out of Andrew Kuchling 'pyCrypto' test
fixture. Having a need to XOR Binascii Hex strings in my current
project, I found it very helpful to cut down on a bit of code
clutter.

So if you have a need for a Crypto module, this one seems to work off
the self without much effort and it comes /w a nice XOR function too
boot. :)

PyCrypto can be found at: http://www.amk.ca/python/code/crypto

import binascii
from Crypto.Cipher import XOR

def die(string):
import sys
print '***ERROR: ', string
# sys.exit(0) # Will default to continuing onward...

testdata_xor = [('ffffffffffffffff',
'a5a5a5a5a5a5a5a5','5a5a5a5a5a5a5a5a')]

for entry in testdata_xor:
key,plain,cipher=entry
key=binascii.a2b_hex(key)
plain=binascii.a2b_hex(plain)
cipher=binascii.a2b_hex(cipher)
obj=XOR.new(key)
ciphertext=obj.encrypt(plain)
print binascii.b2a_hex(ciphertext)

if (ciphertext!=cipher):
die('XOR failed on entry '+`entry`)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

converting strings to hex 10
Python source colorizer 1

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top