using System.Collections.Generic;
using System.Threading.Tasks;
public static class Base64
private static char[] arrBase64 = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '/', '+' };
private static string stringBase64 = "";
private static string byteArrayToString(byte[] source)
source.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
private static IEnumerable<string> stringToArrayXchars(string source, int nbChar)
return Enumerable.Range(0, source.Length / nbChar)
.Select(x => source.Substring(x * nbChar, nbChar));
private static int binaryToInt(string binary)
double multiplier = (double)(binary.Length - 1);
foreach (char c in binary)
int number = (int)char.GetNumericValue(c);
total += number * (int)Math.Pow(2, multiplier);
public static String Encode(byte[] source)
IEnumerable<string> arrayByte6 = stringToArrayXchars(byteArrayToString(source), 6);
foreach (string byte6 in arrayByte6)
stringBase64 += arrBase64.GetValue(binaryToInt(byte6));