Base32Encoding Implementation For .NET

Posted on 12 Jun 2009 09:38 under c# code .net software

I was working in a project which required me to send that data to one application in the form of a series of bytes. This data was to be processed by humans so it was necessary that proper encoding mechanism is used. I considered my alternatives:

  1. Hex Encoding
  2. Base64 Encoding
  3. Base32 Encoding
  4. ZBase32 Encoding

Hex encoding is pretty simple (atleast simple for programmers). You just write the hex string in plain text. Resulting string is case insensitive but too long. If input data length is x bytes, you will end up with 2x character long string.
Base64 encoding is pretty good in terms of efficiency since it uses approx. 4x/3 characters. It uses 0..9, A..Z, a..z and 2 special characters for encoding. This type of encoding is used widely (email processing, keys storage etc). .NET Framework library has Convert class which takes care of this task.

But Base64 encoding is not really suitable for humans considering it uses couple of special characters and it is case sensitive. Imagine someone is speaking the encoded text over phone! So I didn’t settle for this one.

Next one was Base32 encoding which is pretty similar in concept but it uses characters A..Z and 2..7. So it has the advantage of being case in-sensitive at the cost of somewhat bigger encoded text. Text that is encoded with Base32 method has approx 1.6x characters. I was OK with somewhat increased length because ease of use was more important. Exploring further revealed that there is one modified implementation of Base32 encoding which is called zBase32 encoding. This encoding:

  1. Permutes the characters for encoding so that more frequent characters are easier to read (even from a hand scribbled page).
  2. Intelligently calculates the original data length which eliminates the need of padding. Padding increases encoded text length significantly.

So I finally decided to use zBase32 encoding in my project but whoa…I couldn’t find any implementation of zBase32 encoding for .NET. After searching for a while, I decided to write one myself.

Download Base32Encoder.dll

This library is pretty simple. It has 2 classes only:

  1. Base32Encoder: Implements base 32 encoding
  2. ZBase32Encoder: Derived from Base32Encoder! Uses Base32Encoder to implement ZBase32 encoding.

Here is sample code:

 public void Sample () {
    var zBase32 = new ZBase32Encoder ();
 
    byte[] inputData = new byte[] { 0xF0, 0xBF, 0xC7 };
    string encodedText = zBase32.Encode (inputData);
 
    byte[] outputData = zBase32.Decode (encodedText);
 
    Console.WriteLine ("Original: {0}, Encoded: {1}, Decoded: {2}",
        ToString (inputData), encodedText, ToString (outputData));
}

And here is the output of above code:

Original: F0BFC7, Encoded: 6n9hq, Decoded: F0BFC7

If you checkout the samples given on zBase32 website, you will find the same results.

Comparison

Here is a short comparison of encoded text lengths of both base32 encoding (all lengths are in bytes). You can easily realise that normal base32 encoding doesn’t encodes efficiently when input length is not exact multiple of 5.

Input Length Base32 Encoded Text Length zBase32 Encoded Text Length
1 8 2
2 8 4
3 8 5
4 8 7
5 8 8
6 16 10
7 16 12
8 16 13
9 16 15
10 16 16

You can also download complete source if you need.

rating: 0+x
Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +