

Base64 Encoding With C#
source link: https://khalidabuhakmeh.com/base64-encoding-with-csharp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Base64 Encoding With C#
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.
Recommend
-
111
对称加密和Base64编码
-
18
While browsing some API documentation, I saw references to Base64 for passing credentials to the API. I had seen Base64 referenced a few times, but had no idea how to convert text into Base64. So, I did some digging. This post will o...
-
7
The Oracle documentation for java.util.Base64 refers to three different kinds of Base64, but only by reference to RFCs. Here it is in one place.
-
12
Base64 Encoding and Decoding in JavaScript in Adobe AIR Thursday, June 12, 2008 I have been working on a JavaScript AIR app for the past week or so while on the on AIR Tour in Europe, and ran into the need to Base64 enc...
-
12
S M Firoz Ashraf November 26, 2021 2 minute read
-
6
Base64 Encoding and Decoding Using Pythonby Abder-Rahman AliMar 22, 2016(Updated Apr 11, 2022)Read Time:9...
-
7
Cryptopals Crypto Challenges These are solutions to the Cryptopals Crypto Challenges, solved using C# and .NET Core. These articles talk through the theory and code snippets that I found most...
-
7
Encoding Base64/32/16 encoding/decoding for React Native written in C/C++ and JSI May 17, 2022 1 min read
-
6
Base64 encoding: What sysadmins need to know By understanding Base64 encoding, you can apply it to Kubernetes secrets, OpenSSL, e...
-
7
How to upload files from your HTML form using Base64 encodingDecember 22, 2021 · 5 min readMelih EkinciFrontend...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK