ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 网络编程 >> C#教程 >> c#可自定义码表的base64加密解密算法类

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

来源:网络整理     时间:2015-12-16     关键词:

本篇文章主要介绍了"c#可自定义码表的base64加密解密算法类",主要涉及到方面的内容,对于C#教程感兴趣的同学可以参考一下: 000 1using System;2using System.Collections.Generic;3using System.Linq;4using S...

000

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

以上就介绍了c#可自定义码表的base64加密解密算法类,包括了方面的内容,希望对C#教程有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_248571.html

相关图片

相关文章