The post Base64 Encode and Decode in C# appeared first on Tech Insights.
]]>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.
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 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.
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 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.
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:
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); }
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.
The post Base64 Encode and Decode in C# appeared first on Tech Insights.
]]>