Welcome!

Microsoft Cloud Authors: Kevin Benedict, Pat Romanski, Liz McMillan, Lori MacVittie, Elizabeth White

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Cover Story: Understanding Base64 Encoding

What it is, when to use it, and how to write custom Base64 encoding

If you work in a .NET environment you have probably come across Base64 encoded data. For example, Base64 encoding is used in ASP.NET for a Web application's ViewState value, as shown in Figure 1. Base64 encoding is also used to transmit binary data over e-mail. However, if you are like most of my colleagues (and me until recently) you do not have a thorough understanding of precisely what Base64 encoding is and when Base64 encoding should be used. In the this article I will explain exactly what Base64 encoding is, show you how to use the two primary .NET Framework methods that support Base64 encoding and decoding, and present a lightweight, custom C# implementation of Base64 encoding and decoding methods. This article assumes you are a .NET developer, tester, or manager and have intermediate level C# coding skill. After reading the article you'll have a solid grasp of Base64 encoding as well as the ability to write your own custom encoding methods. I think you'll find the ability to use Base64 encoded data is a valuable addition to your skill set.

The best way to show you where I'm headed in this article is with a screenshot. If you examine Figure 2 you'll see that I start with the arbitrary string "Hello" and use it to generate some binary data. After displaying the starting binary data in hexadecimal form, I convert the binary data to a Base64 encoded string using a method from the .NET Framework, and also using my custom encoding method. Notice the encoding results are the same. In the next part of the screenshot in Figure 2 I decode the Base64 encoded strings back to their original byte arrays, using both the built-in .NET Framework method and my custom implementation, and display in hexadecimal form. The complete program, which produced the screenshot in Figure 2, is presented in Listing 3.

What Is Base64 Encoding?
Exactly what is Base64 encoding? Base64 encoding is a scheme that encodes arbitrary binary data as a string composed from a set of 64 characters. The exact character set can be any 64 distinct ASCII characters, but by far the most common set is "A" through "Z," "a" through "z," "0" through "9," "+," and "/." For example, using this character set the 40-bit data:

01001000 01100101 01101100 01101100 01101111

can be Base64 encoded as the string:

SGVsbG8=

The trailing "=" character is a padding character as I will explain shortly. After seeing Base64 encoding for the first time, most engineers have an immediate question: Why would anyone want to use Base64 encoding? Base64 encoding is useful when you want to transmit binary data over a communication channel that is designed to transmit character data. For example, consider e-mail. The e-mail protocol SMTP was originally designed to send and receive only simple text data. However, suppose you want to transmit binary data such as a JPEG image. If you can encode the image as a Base64 string, then you can send the image just like any other message. Another common example is sending an ASP.NET Web application's ViewState value (which is a binary value representing the overall state of the application) over HTTP (which is an inherently text-based transport protocol). But why go to the trouble of Base64 encoding when "ordinary" encoding already exists? By ordinary encoding I mean regular hexadecimal encoding. For example, the 40-bit binary data above can be represented as a hexadecimal string: 48 65 6C 6C 6F (where the spaces are included just for readability). The answer is that Base64 encoding is more efficient than hexadecimal encoding in the sense that Base64 encoding requires fewer characters to represent the same data. Notice that the hexadecimal encoding of the 40-bit data above requires 10 characters while Base64 encoding requires only 8 characters - a 20 percent reduction.

Because Base64 encoding only uses 64 characters, any of the characters can be represented with just 6 bits because 26 = 64. Or, put another way, using 6 bits you can represent data in the range 000000b to 111111b, which is 0d to 63d. This is the key to Base64 encoding efficiency. The best way to explain how Base64 encoding works is with a picture as shown in Figure 3.

Suppose the first three bytes of input to be encoded are 48h, 65h, and 6Ch. In Figure 3 these values are shown in their binary representation: 01001000, 01100101, and 01101100. The first six bits of input, 010010, have value 18d, which in turn maps to character [18] in the Base64 character set, which is "S." The second six bits of input - the last two bits of the first byte of input and the first four bits of the second byte of input - equal 6d, which maps to character "G," and so on. Notice that three bytes of input map neatly to four character of output. Because of this it is convenient to implement Base64 encoding in "blocks" that represent a group of three bytes of input, or four characters of output.

NET Support for Base64 Encoding
The .NET Framework supports Base64 encoding with two methods. The Convert.ToBase64String() method accepts a byte array as an input argument and returns a Base64 encoded string (using the usual 64-character set described in the previous section). The Convert.FromBase64String() accepts a string argument (which is assumed to be a valid Base64 encoded string) and returns the corresponding byte array. Using these two methods is very easy. Notice both methods belong to the Convert class and are static so you do not need to instantiate an object to use the methods. The Convert class is part of the System namespace. Consider this code snippet:

byte[] bytes = new byte[] { 0x5F, 0xC9, 0xBF, 0x17 };
string base64 = Convert.ToBase64String(bytes);
Console.WriteLine(base64);

This code would produce "X8m/Fw==" as output. Notice that the input has size 4 bytes. The first three bytes (3 * 8 = 24 bits) of input are used to produce the first four Base64 characters (4 * 6 = 24 bits) of output. The last input byte produces the rest of the output, and the output is padded with "=" characters to bring the output size up to an even multiple of 4. The technique to decode is similar. The statements

string encoded = "X8m/Fw==";
byte[] result = Convert.FromBase64String(encoded);
Console.WriteLine(BitConverter.ToString(result));

produce "5F-C9-BF-17" as output. The Framework Base64 methods are simple and straightforward. However, if you are encoding, transmitting, and decoding large amounts of binary data, it is up to you write auxiliary code, which buffers the process by breaking the input data into manageable-sized chunks.

A Lightweight Custom Base64 Encoder
The .NET Framework's ToBase64-String() and FromBase64String() methods will meet most of your Base64 encoding needs. However what if you are developing a system and need a slightly different encoding scheme? For instance, you may want to use a different character set than the normal{"A"-"Z," "a"-"z," "0"-9," "+," "/"} set. In this section I'll present a lightweight, custom Base64 encoder written in C# that you can use as a starting point for your own custom encoder. If you search the Internet you'll find quite a few Base64 encoding examples. The one I present here is a hybrid of several I found combined with one I wrote recently, and is designed for maximum clarity rather than for efficiency. The custom encoder is presented in Listing 1.

Because I want my customer encoder to mimic the interface of the Framework encoder, I begin by creating an overall structure of:

public class MyConverter
{
    public static string ToBase64String(byte[] value)
   {
      // implementation goes here
}
} // class MyConverter

Of course there are many other design alternatives, but making the custom encoder signature the same as the Framework's encoder signature makes sense. I begin my encoder implementation by declaring an array of the 64 characters I want to use for my encoding:

char[] base64Chars = new char[]
{ 'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/' };

This array acts as a lookup table to map a decimal value in the range 0 - 63 to a Base64 character. For example, 0 maps to "A," 1 maps to "B," and 26 maps to "a." I use the normal character set but you can use different characters, or change the order for a custom encoding scheme. Next I compute two values that will control the encoding algorithm:

int numBlocks;
int padBytes;
if ((value.Length % 3) == 0)
{
     numBlocks = value.Length / 3;
     padBytes = 0;
}
else
{
     numBlocks = 1 + (value.Length / 3);
     padBytes = 3 - (value.Length % 3);
}

More Stories By James McCaffrey

Dr. James McCaffrey works for Volt Information Sciences, Inc., where he manages technical training for software engineers working at Microsoft's Redmond, WA campus. He has worked on several Microsoft products, including Internet Explorer and MSN Search. James can be reached at [email protected] or [email protected]

Comments (3) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
trash_incinerator 02/19/10 01:17:00 PM EST

With all due respect to the author, your explanation for why Base64 encoding exists is wrong. The "string" of hex for your example is not comprised of 10 characters as you have indicated. Hex uses 2 digits to represent 8 bits. Four bits for the first digit and four bits for the second. There are, in fact, only 5 bytes of data there, whereas the Base64 encoded string is using 8 bytes.

Base64 is NOT a means of compressing data. In fact it makes the information being represented larger. The reason why this is sometimes necessary is because of the fact that systems assign special meanings to specific bytes or byte sequences. In XML for example, there are special bytes that are not considered valid characters in XML. In order to send information in an XML stream with characters that are not allowed, you have to replace the illegal bytes with legal ones. Hence, Base64 allows you to take arbitrary bytes and reassign them in a way that can be reversed later.

Kumanan Murugesan 04/16/08 10:07:55 AM EDT

Dr. James,
Wonderful article. I was wondering what this does and why is it required many times like other folks.

SYS-CON Belgium News Desk 03/19/06 10:04:38 AM EST

If you work in a .NET environment you have probably come across Base64 encoded data. For example, Base64 encoding is used in ASP.NET for a Web application's ViewState value, as shown in Figure 1. Base64 encoding is also used to transmit binary data over e-mail. However, if you are like most of my colleagues (and me until recently) you do not have a thorough understanding of precisely what Base64 encoding is and when Base64 encoding should be used. In the this article I will explain exactly what Base64 encoding is, show you how to use the two primary .NET Framework methods that support Base64 encoding and decoding, and present a lightweight, custom C# implementation of Base64 encoding and decoding methods. This article assumes you are a .NET developer, tester, or manager and have intermediate level C# coding skill. After reading the article you'll have a solid grasp of Base64 encoding as well as the ability to write your own custom encoding methods. I think you'll find the ability to use Base64 encoded data is a valuable addition to your skill set.

IoT & Smart Cities Stories
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...