Base64 Encode and Decode in C#

Base64 encoding and decoding is a common method for translating binary data to text format. It is particularly handy for sending binary data over a network that only supports text data or storing binary data in a database that only allows text data. In this post, we’ll look at how Base64 encode and decode in C#, as well as best practices for incorporating Base64 into your work.

Base64 consists of alphanumeric symbols, along with a few special characters. These symbols can be utilized to store binary data in ASCII string format. Essentially, each Base64 digit corresponds to 6 bits of binary data stored as an 8-bit character in memory.

Let’s practically execute the Base64 Encode and Decode string in C#

Base64 Encode Strings in C#

The ToBase64String() method takes four parameters: inArray, offset, length, and options. Let’s examine each parameter and how valuable it might be.

   public static string Base64Encode(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue);
            return Convert.ToBase64String(encodebyte);

        }

InArray

InArray is a required argument that consists of an array of 8-bit unsigned numbers. For example, in order to convert the string “Normal String Value” to Base64, we must first obtain the bytes. The resulting Base64 output is.

Offset

An optional Int32 parameter that indicates the starting location for encoding. Using the same example of “Normal String Value”, we may set the optional argument to 6 if we simply wish to encode “String Value” To work properly, this argument must be used in conjunction with the length option.

 public static string Base64Encodeoffset(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue,6,13);
            return Convert.ToBase64String(encodebyte);

        }

Length

Length is an optional Int32 parameter that is used in conjunction with the offset parameter. It indicates how many items or variables we wish to encode. In the preceding example, the value of this parameter was 6.

Options

We may use the options parameter to add line breaks to our Base64 output. Line breaks improve the legibility of Base64 text and are useful when using tools that struggle with long lines. If this parameter is not specified, the encoding scheme will insert a line break every 76 characters.

   public static string Base64Encodelinebreak(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue);
            return Convert.ToBase64String(encodebyte,Base64FormattingOptions.InsertLineBreaks);

        }

When encoding Base64 strings in C#, there are a few best practices you should follow:

  • Always use the UTF-8 encoding when converting text to bytes.
  • Only encode small amounts of data at a time to avoid performance issues.
  • Don’t use Base64 for sensitive information, as it can be easily decoded.

Base64 Decode Strings in C#

Decoding a Base64-encoded string to its original form is also straightforward in C#. You can use the Convert.FromBase64String method to convert a Base64 encoded string to a byte array. Here’s an example code snippet that shows how to decode a Base64-encoded string:

  public static string Base64Decode(string stringvalue)
        {
            var decodetext = Convert.FromBase64String(stringvalue);
            return Encoding.UTF8.GetString(decodetext);
        }
C# Base64 Encoding and Decoding

Full Source Code

using System.Text;

namespace Encode_Decode_csharp
{
    internal class program
    {
        static void Main(string[] args)
        {
           
            var encodevalue = Base64Encodelinebreak("Normal String Value");
            Console.WriteLine("Encoded "+ encodevalue);

            var decodevalue = Base64Decode(encodevalue);
            Console.WriteLine("Decoded "+ decodevalue);

            
        }

        public static string Base64Encode(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue);
            return Convert.ToBase64String(encodebyte);

        }
        public static string Base64Decode(string stringvalue)
        {
            var decodetext = Convert.FromBase64String(stringvalue);
            return Encoding.UTF8.GetString(decodetext);
        }

        public static string Base64Encodeoffset(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue,6,13);
            return Convert.ToBase64String(encodebyte);

        }

        public static string Base64Encodelinebreak(string stringvalue)
        {
            var encodebyte = Encoding.UTF8.GetBytes(stringvalue);
            return Convert.ToBase64String(encodebyte,Base64FormattingOptions.InsertLineBreaks);

        }
    }
}

More Post Different Ways to Split a String in C#

Conclusion

In this article, we have gained knowledge on how to perform Base64 encoding and decoding in C#. Furthermore, we have demonstrated the process of encoding a string to Base64 and reversing the process as well.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments