Python’s binascii - hexlify() and unhexlify()
What the heck?

December 9th, 2009 — 12:55 am

Today, a dear friend of mine came up to me and asked about the Python module binascii - particularly about the methods hexlify() and unhexlify(). Since he asked for it, I’m going to share my answer publicly with you.

First of all, I’m defining the used nomenclature:

  • ASCII characters are being written in single quotes
  • decimal numbers are of the type Long with a L suffix
  • hex values have a \x prefix

First, let me quote the documentation:

binascii.b2a_hex(data)
binascii.hexlify(data)
Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.
binascii.a2b_hex(hexstr)
binascii.unhexlify(hexstr)
Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex()hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a TypeError is raised.

I’ll begin with hexlify(). As the documentation states, this method splits a string which consists of hex-tuples into distinct bytes.

The ASCII character ‘A’ has 65L as numerical representation. To verify this in Python:

>>> long(ord('A'))
65L

You might ask “Why is this even relevant to understand binascii?” Well, we don’t know anything about how ord() does its job. But with binascii we can re-calculate manually and verify.

>>> binascii.hexlify('A')
'41'

Now we know that an ‘A’ - interpreted as binary data and shown in hex - resembles ‘41′. But wait, ‘41′ is a string and no hex value! That’s no biggy, hexlify() represents its result as string.

To stay with the example, let’s convert \41 into a decimal number and check if it equals 65L.

>>> long('41', 16)
65L

Tada! It seems that ‘A’ = \41 = 65L.
You might have known that already, but please, stay with me a minute longer.

To make it look a little more complex:

>>> binascii.hexlify('A') == "%X" % long('41', 16)
True

Be aware that

>>> "%X" %n

converts a decimal number into its hex representation.

——

binascii.unhexlify() naturally does the same thing as hexlify(), but in reverse. It takes binary data and displays it in tuples of hex-values.

I’ll start off with an example:

	>>> binascii.unhexlify('41')
	'A'

	>>> binascii.unhexlify("%X" % ord('A'))
	'A'

Here, unhexlify() takes the numerical representation 65L from the ASCII character ‘A’

	>>> ord('A')
	65

converts it into hex \41

	>>> "%X" % ord('A')
	'41'

and represents it as a 1-tuple (meaning dimension of one) of hex values.

And now the conclusio - why might all of this be useful?
Right now, I can think of at least four use cases:

  • cryptography
  • data-transformation (i.e. Base64 for MIME/E-Mail attachements)
  • security (deciphering binary readings off a network, pattern matching, …)
  • textual representation of escape sequences

Taking up the last example, I’ll show you how to visualize the Bell esape sequence (you know, that thing that keeps beeping in your terminal).
Taken from the ASCII table, the numerical representation of the Bell is \07. Programmers might know it better as \a.

	>>> '\07' == '\a'
	True

Presuming you read such a character in some kind of binary data - for example from a socket

	>>> foo = '\07'

and you want to visualize this data

	>>> print foo

you will not get any results - at least none visible. You might hear the Bell sound if you’re not on a silent terminal.

Now, finally - binascii to the rescue:

	>>> binascii.hexlify('\07')
	'07'

Voilà, the dubious string is decrypted.


Requests: 377

Comment » | personal

Backlog
One step ahead aka my new Mini Cooper

November 10th, 2009 — 11:22 am

It’s finally time to announce at least some news: I’ve got myself a new means of locomotion - no more procrastinating and slacking, because there’s just no train when I need one(; More importantly no more driving on Swiss ice on only two wheels!

Pictures will do the proper talking, even though I have not yet been able to get a good shot in decent sunlight. I will redo these anyway when the optical modding is finished - those custom rallye stripes keep calling me at night!

It might be noteworthy that my dear friend Falko has aquired a 325i just two days after we picked up the Mini. Our ongoing quest for parallelism is still unbroken^^


Requests: 178

3 comments » | personal

Disable Mail-Forwarding for Lotus Notes programmatically

June 29th, 2009 — 08:53 pm

Lotus Notes has a nifty feature to lull managers into false safety: for volatile/unsafe e-mails (or users), it let’s you disable printing/forwarding and copying to clipboard. This can be done using rules, on the SMTP server and on a per e-mail basis. When writing somebody you really don’t trust with some information (but in his inability to spread the word otherwise - by copy/pasting for example), writing a mail would look like this:

prevent_copying

Now, if your victim wants to forward your mail, Lotus Notes would respond with a little pop-up:

success

This certainly looks like a magical and proprietary feature, doesn’t it?  Let’s look at the source of such a “mail”(aka memo in Notus’ language) - you will have to forward it to another mail-client though, because memos can’t be displayed in source:

...
Subject: Testnachricht
MIME-Version: 1.0
Sensitivity: Private
X-Mailer: Lotus Notes Release 6.5.5  CCH1 March 07, 2006
...

As you can see, there is a proprietary meta-flag Sensitivity: Private. It can be reproduced with any decent mail user agent or programmatically. What follows is a little Python code snippet that just does the trick:

import smtplib
from email.message import Message
msg = Message()
msg.set_payload("Testmessage Body")
msg["Subject"] = "Testmessage from Python"
msg["From"] = "preek@dispatched.ch"
msg["To"] = "somebody@somewhere.com"
msg["Sensitivity"] = "Private"
smtp = smtplib.SMTP("localhost")
smtp.sendmail("preek@dispatched.ch", "somebody@somewhere.com", msg.as_string())

But please, don’t use this information unless you absolutely have to. Lotus Notes.. *brr*.

Enjoy(;

If you liked this article, please feel free to re-tweet it and let others know.

twitter_preek

Requests: 289

1 comment » | articles

pTwittery.com released
Convert your Twitter timeline into a diary

June 25th, 2009 — 10:20 pm

I just released my newest program - pTwittery.com
It’s now online, but still in beta stage.

With pTwittery.com you may export all your tweets (aka timeline) into a diary. Your personal diary will then be sent to you via e-mail.

Enjoy, everyone(;

<a href=

If you liked this article, please feel free to re-tweet it and let others know.


    twitter_preek

Requests: 103

Comment » | articles

Server outage

June 25th, 2009 — 09:31 pm

Unfortunately my SUN Fire 280R has left me for good last friday. Let’s all give it a silent two minutes..

After a couple of dear years - spent configuring a nice little Solaris 10 setup, all I have left are a couple of zip files and sql dumps that were supposed to be my backup. And, of course, a lot of memories of that beast being installed by Felix, Falko and me in the first place.. Well.. those were the times - us sitting in the cellar, listening to the sound of Sun SPARC and DEC Alpha hardware - hooked up to private gigabit, trying to figure out why the hell everything is so different in Solaris than we were used to. Heck, it was a nice trip.

But I’m glad to finally put an end to this long gone era. I’m back on a rented dedicated server - running good old Debian stable; as I am supposed to.


Requests: 110

Comment » | personal

Nach oben

« Previous Entries