469,271 Members | 1,711 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

XOR Binascii Hex Strings using the PyCrypto module


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`)

Feb 16 '07 #1
0 1878

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Larry Bates | last post: by
1 post views Thread by Jeremy | last post: by
11 posts views Thread by dirvine | last post: by
2 posts views Thread by Ed Swarthout | last post: by
2 posts views Thread by Mike Meng | last post: by
2 posts views Thread by Ning | last post: by
3 posts views Thread by mirandacascade | last post: by
5 posts views Thread by mirandacascade | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.