C# .NET: Encrypt/Decrypt a Base64 String in .NET

11/23/2013 8:20:17 PM

Base64 Encoding is used to represent binary data in as ASCII string format. The following will take a string and either encrypt into a Base64 string or decrypt from a Base64 string.

It can also be used for very simple encryption. Never use it for data that needs to stay private.

public static string EncryptBase64(string value)
{
	string result = "";

	try
	{
		if (!string.IsNullOrEmpty(value))
		{
			byte[] bytes = System.Text.UnicodeEncoding.UTF8.GetBytes(value);
			result = Convert.ToBase64String(bytes);
		}
	}
	catch{}

	return result;
}


public static string DecryptBase64(string value)
{
	string result = "";

	try
	{
		byte[] bytes = Convert.FromBase64String(value);

		result = System.Text.Encoding.UTF8.GetString(bytes);
	}
	catch{}

	return result;
}