How to encode and decode a base64 string in C#

By FoxLearn 1/31/2024 9:42:23 AM   249
How to encode and decode the string to base64 format in c# using ToBase64String and FromBase64String methods
//c# base64 encode byte array
public string Base64Encode(string plainText)
{
    var data = System.Text.Encoding.UTF8.GetBytes(plainText);
    return System.Convert.ToBase64String(data);
}

Converting your plaint text to byte array using Encoding.UTF8.GetBytes method, finally convert from byte array to base 64 string using Convert.ToBase64String method.

How to decode a base64 string in c#

//encode decode base64 c# example
public string Base64Decode(string base64Data)
{
    var base64Bytes = System.Convert.FromBase64String(base64Data);
    return System.Text.Encoding.UTF8.GetString(base64Bytes);
}

Converting from base 64 encode data to byte array using Convert.FromBase64String method, finally using Encoding.UTF8.GetString method to convert string

Related