Welcome!

Microsoft Cloud Authors: Andreas Grabner, Stackify Blog, Liz McMillan, David H Deans, Automic Blog

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.

@ThingsExpo Stories
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend @CloudExpo | @ThingsExpo, June 6-8, 2017, at the Javits Center in New York City, NY and October 31 - November 2, 2017, Santa Clara Convention Center, CA. Learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that Interoute has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Interoute is the owner operator of Europe's largest network and a global cloud services platform, which encompasses over 70,000 km of lit fiber, 15 data centers, 17 virtual data centers and 33 colocation centers, with connections to 195 additional partner data centers. Our full-service Unifie...
SYS-CON Events announced today that Progress, a global leader in application development, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Enterprises today are rapidly adopting the cloud, while continuing to retain business-critical/sensitive data inside the firewall. This is creating two separate data silos – one inside the firewall and the other outside the firewall. Cloud ISVs ofte...
SYS-CON Events announced today that delaPlex will exhibit at SYS-CON's @ThingsExpo, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. delaPlex pioneered Software Development as a Service (SDaaS), which provides scalable resources to build, test, and deploy software. It’s a fast and more reliable way to develop a new product or expand your in-house team.
SYS-CON Events announced today that Hitachi Data Systems, a wholly owned subsidiary of Hitachi LTD., will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City. Hitachi Data Systems (HDS) will be featuring the Hitachi Content Platform (HCP) portfolio. This is the industry’s only offering that allows organizations to bring together object storage, file sync and share, cloud storage gateways, and sophisticated search and...
Existing Big Data solutions are mainly focused on the discovery and analysis of data. The solutions are scalable and highly available but tedious when swapping in and swapping out occurs in disarray and thrashing takes place. The resolution for thrashing through machine learning algorithms and support nomenclature is through simple techniques. Organizations that have been collecting large customer data are increasingly seeing the need to use the data for swapping in and out and thrashing occurs ...
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
Detecting internal user threats in the Big Data eco-system is challenging and cumbersome. Many organizations monitor internal usage of the Big Data eco-system using a set of alerts. This is not a scalable process given the increase in the number of alerts with the accelerating growth in data volume and user base. Organizations are increasingly leveraging machine learning to monitor only those data elements that are sensitive and critical, autonomously establish monitoring policies, and to detect...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with the 21st International Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. @ThingsExpo Silicon Valley Call for Papers is now open.
DevOps is often described as a combination of technology and culture. Without both, DevOps isn't complete. However, applying the culture to outdated technology is a recipe for disaster; as response times grow and connections between teams are delayed by technology, the culture will die. A Nutanix Enterprise Cloud has many benefits that provide the needed base for a true DevOps paradigm.
SYS-CON Events announced today that SoftLayer, an IBM Company, has been named “Gold Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2016, at the Javits Center in New York, New York. SoftLayer, an IBM Company, provides cloud infrastructure as a service from a growing number of data centers and network points of presence around the world. SoftLayer’s customers range from Web startups to global enterprises.
In his keynote at @ThingsExpo, Chris Matthieu, Director of IoT Engineering at Citrix and co-founder and CTO of Octoblu, focused on building an IoT platform and company. He provided a behind-the-scenes look at Octoblu’s platform, business, and pivots along the way (including the Citrix acquisition of Octoblu).
SYS-CON Events announced today that DivvyCloud will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. DivvyCloud software enables organizations to achieve their cloud computing goals by simplifying and automating security, compliance and cost optimization of public and private cloud infrastructure. Using DivvyCloud, customers can leverage programmatic Bots to identify and remediate common cloud problems in rea...
SYS-CON Events announced today that Tintri, Inc, a leading provider of enterprise cloud infrastructure, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Tintri offers an enterprise cloud platform built with public cloud-like web services and RESTful APIs. Organizations use Tintri all-flash storage with scale-out and automation as a foundation for their own clouds – to build agile development environments...
SYS-CON Events announced today that Carbonite will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Carbonite protects your entire IT footprint with the right level of protection for each workload, ensuring lower costs and dependable solutions with DoubleTake and Evault.
SYS-CON Events announced today that Technologic Systems Inc., an embedded systems solutions company, will exhibit at SYS-CON's @ThingsExpo, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Technologic Systems is an embedded systems company with headquarters in Fountain Hills, Arizona. They have been in business for 32 years, helping more than 8,000 OEM customers and building over a hundred COTS products that have never been discontinued. Technologic Systems’ pr...
SYS-CON Events announced today that Tappest will exhibit MooseFS at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. MooseFS is a breakthrough concept in the storage industry. It allows you to secure stored data with either duplication or erasure coding using any server. The newest – 4.0 version of the software enables users to maintain the redundancy level with even 50% less hard drive space required. The software func...
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloudistics delivers a complete public cloud experience with composable on-premises infrastructures to medium and large enterprises. Its software-defined technology natively converges network, storage, compute, virtualization, and management into a ...
SYS-CON Events announced today that EARP will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. "We are a software house, so we perfectly understand challenges that other software houses face in their projects. We can augment a team, that will work with the same standards and processes as our partners' internal teams. Our teams will deliver the same quality within the required time and budget just as our partn...
DevOps at Cloud Expo – being held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real r...