c#可自定义码表的base64加密解密算法类

2/11/2016来源:C#应用人气:3861

000

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace Libraries
  8 {
  9     public class Base64Crypt
 10     {
 11         PRivate string S;
 12         private string K;
 13         private List<char> T;
 14         public Base64Crypt()
 15         {
 16             T = new List<char>();
 17             K = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやよらりるれろわをぐげござじずぞだぢづでばびぶべぱぴぷぺぽ";
 18             //K = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//标准码表
 19         }
 20         public string Token
 21         {
 22             get
 23             {
 24                 return S == null ? K : S;
 25             }
 26             set
 27             {
 28                 T.Clear();
 29                 S = value;
 30                 if (S == null)
 31                 {
 32                     foreach (var item in K)
 33                     {
 34                         T.Add(item);
 35                     }
 36                 }else if (S.Length < 64)
 37                 {
 38                     foreach (var item in S)
 39                     {
 40                         T.Add(item);
 41                     }
 42                     for (int i = 0; i < 64-S.Length; i++)
 43                     {
 44                         T.Add(K[i]);
 45                     }
 46                 }
 47                 else
 48                 {
 49                     for (int i = 0; i < 64; i++)
 50                     {
 51                         T.Add(S[i]);
 52                     }
 53                 }
 54             }
 55         }
 56 
 57         public string Encode(string x)
 58         {
 59             return string.IsNullOrEmpty(x) ? x : InternalEncode(Encoding.UTF8.GetBytes(x));
 60         }
 61         public string Decode(string x)
 62         {
 63             return string.IsNullOrEmpty(x) ? x : Encoding.UTF8.GetString(InternalDecode(x));
 64         }
 65 
 66         public byte[] Encode(byte[] x)
 67         {
 68             return x == null ? null : Encoding.UTF8.GetBytes(InternalEncode(x));
 69         }
 70         public byte[] Decode(byte[] x)
 71         {
 72             return x == null ? null : InternalDecode(Encoding.UTF8.GetString(x));
 73         }
 74         private void CheckToken()
 75         {
 76             if (T.Count != 64)
 77             {
 78                 Token = K;
 79             }
 80         }
 81         private byte[] InternalDecode(string x)
 82         {
 83             CheckToken();
 84             byte[] r;
 85             string t;
 86             int p = 0;
 87             int m = x.Length / 4;
 88             int n = x.Length % 4;
 89             if (n == 0)
 90             {
 91                 r = new byte[3 * m];
 92             }
 93             else
 94             {
 95                 r = new byte[3 * m + n-1];
 96                 t = string.Empty;
 97 
 98                 for (int i = n; i > 0; i--)
 99                 {
100                     t += ByteToBin((byte)T.IndexOf(x[x.Length - i])).Substring(2);
101                 }
102 
103                 for (int i = 0; i < n-1 ; i++)
104                 {
105                     r[3 * m + i] = BinToByte(t.Substring(8 * i, 8));
106                 }
107             }
108             for (int i = 0; i < m; i++)
109             {
110                 t = string.Empty;
111                 for (int j = 0; j < 4; j++)
112                 {
113                     t += ByteToBin((byte)T.IndexOf(x[4*i+j])).Substring(2);
114                 }
115                 for (int j = 0; j < t.Length/8; j++)
116                 {
117                     r[p++] = BinToByte(t.Substring(8*j,8));
118                 }
119             }
120             return r;
121         }
122         private string InternalEncode(byte[] x)
123         {
124             CheckToken();
125             string r = string.Empty;
126             string t;
127             int m = x.Length / 3;
128             int n = x.Length % 3;
129             for (int i = 0; i < m; i++)
130             {
131                 t = string.Empty;
132                 for (int j = 0; j < 3; j++)
133                 {
134                     t += ByteToBin(x[3 * i + j]);
135                 }
136                 r += base64Encode(t);
137             }
138 
139             if (n == 1)
140             {
141                 t = ByteToBin(x[x.Length-1]).PadRight(12,'0');
142                 r += base64Encode(t);
143             }
144             else if (n == 2)
145             {
146                 t = string.Empty;
147                 for (int i = n; i > 0; i--)
148                 {
149                     t += ByteToBin(x[x.Length - i]);
150                 }
151                 t = t.PadRight(18,'0');
152                 r += base64Encode(t);
153             }
154             return r;
155         }
156         private string base64Encode(string x)
157         {
158             string r = string.Empty;
159             for (int i = 0; i < x.Length / 6; i++)
160             {
161                 r += T[BinToByte(x.Substring(6 * i, 6))];
162             }
163             return r;
164         }
165         
166         private string ByteToBin(byte x)
167         {
168             return Convert.ToString(x,2).PadLeft(8,'0');
169         }
170         private byte BinToByte(string x)
171         {
172             return Convert.ToByte(x,2);
173         }
174 
175     }
176 }
177