To encode and decode Base64 in C# and VB.NET you can use the following snippet.

Samples

Sample C#

public string EncodeBase64(string input)
{
   return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input));
}

public string DecodeBase64(string input)
{
   return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(input));
}

Sample VB.NET

Public Function EncodeBase64(input As String) As String
	Return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(input))
End Function

Public Function DecodeBase64(input As String) As String
	Return System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(input))
End Function

 

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

 

One thought on “How to encode and decode Base64 in C# and VB.NET”

Leave a Reply