I was working with the Spotify API and reading through the authorization documentation. Like most APIs, we need to securely access the service, authorized as the client application or on behalf of a user. We’ll need to deal with secret tokens and likely encode them into a Base64
string in that process. The client credentials flow requires we concatenate the service provided secrets of clientId
and clientSecret
, after which we Base64 encode the result.
The authorization step made me wonder, “why the heck do we need to Base64 encode these values in the first place?” In this post, we’ll learn why Base64 encoding is necessary and how to perform encoding and decoding in C#.
What Is Base64?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. The design of Base64 helps transmit binary formats across transports that only reliably support text content. Some examples include HTTP and SMTP. We can also see the encoding commonly used in web development, as Base64 values can represent images in cascading style sheets (CSS) and HTML pages.
While values Base64 encoded values are illegible, the encoding process shouldn’t be confused for security. As we’ll see later in this post, we can decode encoded values back to their original value.
To read more on the Base64 encoding, I recommend the Wikipedia article.
Encoding A String
As this section’s title may hint at, we’ll need to choose an Encoding
that will represent our final result. Folks will need to check their API documentation, but ASCII
is a safe bet when encoding values. Let’s take a look at the C# code.
using System;
using System.Text;
using static System.Console;
string khalid;
khalid = nameof(khalid);
byte[] bytes;
bytes = Encoding.ASCII.GetBytes(khalid);
var base64 = Convert.ToBase64String(bytes);
WriteLine(base64);
Note that we’re using the System.Text.Encoding
class and the System.Convert
class to get the bytes representing our string. Remember that Base64 is a binary representation scheme, and bytes are binary (1’s and 0’s). We then use the ToBase64String
method to finish the process. The result of our code is as follows.
a2hhbGlk
Remember, this is a reversible process. Let’s see how we can decode the value of a2hhbGlk
back to khalid
.
Decoding A String
Similar to encoding to Base64, we’ll be using the System.Convert
helper class, along with System.Text.Encoding
. Let’s update our sample.
using System;
using System.Text;
using static System.Console;
string khalid;
khalid = nameof(khalid);
byte[] bytes;
bytes = Encoding.ASCII.GetBytes(khalid);
var base64 = Convert.ToBase64String(bytes);
WriteLine(base64);
// decoding starts here
bytes = Convert.FromBase64String(base64);
var decoded = Encoding.ASCII.GetString(bytes);
WriteLine(decoded);
Running the code above results in the following console output.
a2hhbGlk
khalid
Decoding is a bit more challenging as we need to know the original encoding of our actual value. Additionally, since we are dealing with a byte[]
in the intermediary step, we also need to know the final target type. In our case, we know we’ll be decoding a string
, but we could also be decoding images, audio, or other complex types.
Conclusion
Base64 encoding is everywhere if you know where to look, and it’s an essential part of reliably passing information on the web. When working with APIs using the HTTP protocol, we can expect to perform some encoding/decoding. Hopefully, this post explained why it’s necessary to do so. With a few lines of C# code, we can encode and encode data simply. I hope you enjoyed this post, and please leave a comment about the places you use Base64 encoding/decoding.