Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#

Base32 encoding implementation in .NET

Rate me:
Please Sign up or sign in to vote.
3.54/5 (9 votes)
15 Apr 2009CPOL2 min read 63.9K   2.4K   17   5
Native .NET (C#) implementation of Base32 and zBase32 encoding.

Introduction

This article presents a library which implements Base32 encoding and zBase32 encoding using C#.

Background

I was working on a project where I needed to send some binary information to my application and that information was to be processed by humans. I had the following considerations:

  1. Resulting string should be case insensitive.
  2. Resulting string should be as short as possible.
  3. Resulting string should not contain potentially confusing characters such as o, 0, i, l, 1 etc.

I had the choice of Hex encoding, Base64 encoding, and Base32 encoding. Base64 encoding was most efficient as far as length of output is concerned, but since it produces case sensitive encoded strings, I had to let it go.

Hex encoding produces encoded strings which are too long (double the size of the original data) so this option was also down. Base32 encoding seemed good enough, but Wikipedia told me that there is a modified version of Base32 encoding called zBase32 encoding which has the following advantages over plain Base32 encoding:

  1. It reduces the number of potentially confusing characters.
  2. It permutes the character table so that easier character occurs more frequently.
  3. It uses an intelligent padding scheme (or rather it does not use padding) which results in shorter encoded text in some cases.

I decided to use zBase32 encoding, but I couldn't find any standard implementation on the .NET platform so I ended up writing the implementation myself.

Using the code

This library is pretty simple by any standards. It has only two classes:

  • Base32Encoder: Standard Base32 encoding implementation.
  • ZBase32Encoder: Derived from Base32Encoder. Uses the base class for the modified zBase32 encoding implementation.

Using any of the above encoder is identical. It can be done like this:

C#
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));
}

The output of the above code is:

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

Points of interest

The thing to note here is the difference between Base32 encoding and zBase32 encoding when it comes to the length of the output text. Here is the summary:

Input lengthBase32 encoded text lengthzBase32 encoded text length
182
284
385
487
588
61610
71612
81613
91615

When the input data length is not natural (multiple of 5), Base32 encoding takes way too many characters to encode the same text.

History

  • First revision of the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
I am programmer by profession and by heart. I just love it. I cant believe I am paid for programming. Smile | :)

Currently I have the responsibility of leading a software development team and one product. My work involves dealing with Delphi, .NET (C#) and SQL Server.

More on my home page

Comments and Discussions

 
GeneralMy vote is excellent Pin
goracio8-Feb-11 1:45
goracio8-Feb-11 1:45 
GeneralNo good if new to .NET Pin
stayko3-Jan-11 18:40
stayko3-Jan-11 18:40 
GeneralAlrnative more flexible .net Base32 encoder Pin
mhano30-Apr-10 2:00
mhano30-Apr-10 2:00 
GeneralMy vote of 2 Pin
phi10109-Nov-09 5:38
phi10109-Nov-09 5:38 
General[My vote of 1] Show and explain the code Pin
PIEBALDconsult15-Apr-09 9:53
mvePIEBALDconsult15-Apr-09 9:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.