Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text;
- namespace B64Del
- {
- class Program
- {
- private static char[] arr =
- {
- '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', '0', '1',
- '2', '3', '4', '5', '6', '7', '8', '9', '+',
- '/'
- };
- private static string StrToBin(string str)
- {
- string bin = "";
- foreach (char c in str)
- {
- bin += Convert.ToString((int) c, 2).PadLeft(8, '0');
- }
- return bin;
- }
- private static string BinToLet(string bin)
- {
- return arr[Convert.ToInt32(bin, 2)].ToString();
- }
- private static int DecideOverLen(int len)
- {
- if (len % 6 == 0) return len / 6;
- else return len / 6 + 1;
- }
- private static string B64Encrypt(string str)
- {
- string binaryStr = StrToBin(str);
- string[] thing = new string[DecideOverLen(binaryStr.Length)];
- // forming bin str
- for (int i = 0; i < thing.Length; i++)
- {
- for (int j = 0; j < 6; j++)
- {
- try
- {
- thing[i] += binaryStr[i * 6 + j];
- } catch {}
- }
- }
- string res = "";
- // forming b64 res
- foreach (string bin in thing)
- {
- res += BinToLet(bin + String.Join("", Enumerable.Repeat("0", 6 - bin.Length)));
- }
- // adding = as padding
- res += String.Join("", Enumerable.Repeat("=", 4 - res.Length % 4));
- return res;
- }
- private static void Main(string[] args)
- {
- Console.WriteLine(B64Encrypt("hello world"));
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement