A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Base64 Encode Decode in C# page! Here, you'll find the source code for this program as well as a description of how the program works.
using System.Text;
public class Base64EncodeDecode
{
public static void Usage()
{
Console.WriteLine("Usage: please provide a mode and a string to encode/decode");
Environment.Exit(1);
}
private static bool IsValidBase64(string input)
{
if (string.IsNullOrWhiteSpace(input) || input.Length % 4 != 0)
return false;
foreach (char c in input)
{
if (!"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".Contains(c))
return false;
}
int padCount = input.EndsWith("==") ? 2 :
input.EndsWith('=') ? 1 : 0;
int firstPadIndex = input.IndexOf('=');
return firstPadIndex == -1 || firstPadIndex >= input.Length - padCount;
}
public static int Main(string[] args)
{
if (args.Length != 2)
{
Usage();
return 1;
}
string mode = args[0].ToLowerInvariant();
string value = args[1];
if (string.IsNullOrWhiteSpace(mode) || string.IsNullOrWhiteSpace(value))
{
Usage();
return 1;
}
try
{
string result = mode switch
{
"encode" => Convert.ToBase64String(Encoding.UTF8.GetBytes(value)),
"decode" => IsValidBase64(value)
? Encoding.UTF8.GetString(Convert.FromBase64String(value))
: throw new ArgumentException("Input is not valid Base64."),
_ => throw new ArgumentException("Unknown mode. Use 'encode' or 'decode'.")
};
Console.WriteLine(result);
return 0;
}
catch
{
Usage();
return 1;
}
}
}
Base64 Encode Decode in C# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.