473,503 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2285

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
9783
by: Larry Bates | last post by:
I'm trying to get the results of binascii.crc32 to match the results of another utility that produces 32 bit unsigned CRCs. binascii.crc32 returns results in the range of -2**31-1 and 2**21-1....
1
2246
by: Jeremy | last post by:
I'm working on a project to create a keyfinder program that finds the Windows CD Key in the registry and decodes it. I prototyped it in Python and it worked great but for several reasons I've...
11
3014
by: dirvine | last post by:
Does anyone know if pycrypto is active at all. I have been browsing groups etc. for some info and have found entries from 2003 (latest) regarding some bits I was looking for in particular reference...
2
1848
by: Ed Swarthout | last post by:
Why is there no binascii.a2b_binary(bitstr) which returns the binary data represented by the bit string? Like: >>> binascii.a2b_binary('0011001100110101') '35' perl has pack("B*",...
2
2485
by: Mike Meng | last post by:
Hi all, I'm learning Twisted and downloaded pyOpenSSL and pycrypto win32 installer on http://twisted.sourceforge.net/contrib/ . But I find the lastest version are for Python 2.3. I try to...
2
3635
by: Ning | last post by:
I'm trying to write an IM client which sends encrypted messages to the server. I tried to use pycrypto library, but when I came to 3DES cypher I was confused about the keysize to use. In the...
3
4306
by: mirandacascade | last post by:
Operating system: Win XP Vsn of Python: 2.4 Situation is this: Required to calcluate a message digest. The process for calcluating the digest must use an SHA-256 algorithm. Questions: 1) Is...
5
12496
by: mirandacascade | last post by:
Attempting to determine whether the PyCrypto package has the capability to perform AES256 encryption. I received the following C# snippet: CryptoProvider provider = new CryptoProvider();...
6
5197
by: Vishal | last post by:
Hi, I have a file with a long list of hex characters, and I want to get a file with corresponding binary characters here's what I did: .... x = line.rstrip('\n') .... y =...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.