zoukankan      html  css  js  c++  java
  • Base64

            private string Encode(string str)
            {
                byte[] buffer = Encoding.Default.GetBytes(str);
    
                byte b1, b2, b3, c1, c2, c3, c4;
                string mes = string.Empty;
                int count = buffer.Length / 3;
                int resst = buffer.Length % 3;
    
                for (int i = 0; i < count; i++)
                {
                    int j = i * 3;
                    b1 = buffer[j];
                    b2 = buffer[j + 1];
                    b3 = buffer[j + 2];
    
                    c1 = (byte)(b1 >> 2);
                    c2 = (byte)(((b1 & 3) << 4) + (b2 >> 4));
                    c3 = (byte)(((b2 & 15) << 2) + (b3 >> 6));
                    c4 = (byte)(b3 & 63);
                    mes += GetChar(c1, c2, c3, c4);
                }
    
                if (resst == 1)
                {
                    b1 = buffer[buffer.Length - 1];
                    c1 = (byte)(b1 >> 2);
                    c2 = (byte)((b1 & 3) << 4);
                    c3 = 64;
                    c4 = 64;
                    mes += GetChar(c1, c2, c3, c4);
                }
                else if (resst == 2)
                {
                    b1 = buffer[buffer.Length - 2];
                    b2 = buffer[buffer.Length - 1];
                    c1 = (byte)(b1 >> 2);
                    c2 = (byte)(((b1 & 3) << 4) + (b2 >> 4));
                    c3 = (byte)((b2 & 15) << 2);
                    c4 = 64;
                    mes += GetChar(c1, c2, c3, c4);
                }
    
                return mes;
            }
    
            private string Decode(string str)
            {
                byte[] buffer = Encoding.Default.GetBytes(str);
    
                byte b1, b2, b3, c1, c2, c3, c4;
                string mes = string.Empty;
                int count = buffer.Length / 4;
                int resst = buffer.Length % 4;
    
                for (int i = 0; i < count; i++)
                {
                    int j = i * 4;
                    c1 = GetIndex(buffer[j]);
                    c2 = GetIndex(buffer[j + 1]);
                    c3 = GetIndex(buffer[j + 2]);
                    c4 = GetIndex(buffer[j + 3]);
    
                    b1 = (byte)((c1 << 2) + (c2 >> 4));
                    if (c3 != 64 && c4 != 64)
                    {
                        b2 = (byte)(((c2 & 15) << 4) + (c3 >> 2));
                        b3 = (byte)(((c3 & 3) << 6) + c4);
                        mes += Convert.ToChar(b1);
                        mes += Convert.ToChar(b2);
                        mes += Convert.ToChar(b3);
                    }
                    else if (c3 != 64 && c4 == 64)
                    {
                        b2 = (byte)(((c2 & 15) << 4) + (c3 >> 2));
                        mes += Convert.ToChar(b1);
                        mes += Convert.ToChar(b2);
                    }
                    else
                        mes += Convert.ToChar(b1);
                }
    
                return mes;
            }
    
            private string GetChar(byte c1, byte c2, byte c3, byte c4)
            {
                return new string(new char[] { cs[c1], cs[c2], cs[c3], cs[c4] });
            }
    
            private byte GetIndex(byte c)
            {
                for (byte i = 0; i < cs.Length; i++)
                {
                    if (c == cs[i])
                        return i;
                }
                return 0;
            }
            private char[] cs = 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',
                '0','1','2','3','4',
                '5','6','7','8','9',
                '+','/','='};
  • 相关阅读:
    Codeforces 877 C. Slava and tanks
    Codeforces 877 D. Olya and Energy Drinks
    2017 10.25 NOIP模拟赛
    2017 国庆湖南 Day1
    UVA 12113 Overlapping Squares
    学大伟业 国庆Day2
    51nod 1629 B君的圆锥
    51nod 1381 硬币游戏
    [JSOI2010]满汉全席
    学大伟业 2017 国庆 Day1
  • 原文地址:https://www.cnblogs.com/bloodofhero/p/2882619.html
Copyright © 2011-2022 走看看