c sharp

How to do Base64 Encoding and Decoding in C#?

C# is an extremely popular high-level language used in today’s world of programming. It is used very extensively for designing web applications; however, apart from this, it can also be used for serving some seemingly small purposes, such as performing the Base64 coding of important data. Therefore, today, we will be talking about the methods of doing the Base64 encoding and decoding in C# by sharing with you the complete source code and its execution details.

Why have we picked up C# for doing the Base64 Encoding and Decoding?

You might be thinking that when there are large numbers of other programming languages available out there in the market, then why do we choose to teach you the Base64 encoding and decoding in C#. Well, we will try to answer this question in this section of our article. When it comes to naming the top high-level programming languages, then C# makes its way to this list very easily. There is absolutely no doubt about it. This is one reason behind selecting this programming language.

The other reason is that the syntax of C# has a very close resemblance with Java as well as with the traditional programming languages C and C++. Because of this, even if a programmer knows the basics of either of these languages, he can quickly get his hands on the C# programming language and that too without any difficulty. Moreover, even the old-school programmers find this language quite user-friendly. That is exactly why today, we wish to share the method of the Base64 encoding and decoding with you by using the C# programming language.

Compiler Used

We have used an online compiler for writing and executing the C# script for doing the Base64 encoding and decoding.

However, you can also use any other compiler of your choice if you do not want to use an online compiler such as Visual Studio, Visual Studio Code, etc.

The General Process of the Base64 Encoding and Decoding in C#

Before sharing with you the actual C# script for the Base64 encoding and decoding, we would like to give you a brief overview of the general process of the Base64 coding. For encoding, C# first converts the desired data into UTF8 bytes and then encodes these bytes in the form of a string. Then, for the Base64 decoding, the encoded string is converted into UTF8 bytes, and then those bytes are converted back to the original string. Now, it will be relatively easier for you to understand the script shared below.

C# Script for the Base64 Encoding and Decoding

For performing the Base64 encoding and decoding in C#, we have written a complete script that is shown in the images below:

In the C# script shown in the images above, we began by including the two libraries, i.e., “System” and “System.Text,” that will help us in carrying out the Base64 encoding and decoding in this C# script. Then, we have created a public class named “EncodeDecode” that will contain our whole code. Within this class, we have a function named “ToBase64Encode” that accepts a string argument, i.e., the string to be encoded.

The return type of this function is also a string, i.e., it will return the encoded string. In this function, we have first checked whether the passed string is empty or not. If the string is empty, then it will be returned as it is without any encoding. However, if the input string is non-empty, then it will first be converted into UTF8 code and will be saved in a byte array. After that, the encoded bytes will be returned by this function after being converted to a string.

Then, we have created a function named “ToBase64Decode” that takes an encoded string as an input and returns the decoded string. Inside this function, again, we have checked whether the encoded string is empty or not. If it is empty, then the encoded string will be returned as it is without decoding. However, if it is non-empty, then first, the encoded string will be converted to UTF8 bytes, after which these bytes will be decoded as a normal string, and the decoded string will be returned by this function.

We have a “main()” function inside this class too. A sample string is defined inside this function. Then, we used the command for displaying this string on the console. After that, we have defined a string named “encoded” and have equalized it to the result returned by the encoding function. Then, we printed the result of this string on the console. In the same manner, we have created another string named “decoded” for holding the result of the decoding function. Then, we have just printed this string on the console.

Result of the C# Script for the Base64 Encoding and Decoding

For executing this C# script, we have used the “Run” button present within the window of our online compiler. This button is also highlighted in the image shown above. The result of the C# script for the Base64 encoding and decoding is shown in the following image:

The C# script that we have just designed printed the original string first, after which it printed the encoded string followed by the decoded string. Since the decoded and the original strings are the same, therefore, we can say that this script perfectly performed the Base64 encoding and decoding in C#.

Conclusion

This article was meant to provide you with the relevant knowledge regarding performing the Base64 encoding and decoding in C#. However, since we solely wanted to focus on the method of the Base64 coding in C#, therefore, we did not install an independent compiler for it; rather, we have used an online compiler. After going through the code provided in this article, you will be able to perform the Base64 encoding and decoding of your desired data very effectively in C#.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.