Manage base64 encoding with ASP.NET Core

The class System.Text.Encoding provides all you need to encrypt to base64 encoding and decrypt from base64 encoding.

Convert string to base64


string txt = "Test";
byte[] encodedBytes = System.Text.Encoding.Unicode.GetBytes(txt);
string encodedTxt = Convert.ToBase64String(encodedBytes);

Convert base64 string to string


string txt = "Test";
byte[] decodedBytes = Convert.FromBase64String(Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(txt)));
string decodedTxt = System.Text.Encoding.UTF8.GetString(decodedBytes);
string decodedTxt2 = System.Text.Encoding.Unicode.GetString(decodedBytes);
29/10/2016
  • ASP.NET Core
  • base64