首页 > 编程 > .NET > 正文

DotNet中用到的加密算法总结

2024-07-21 02:28:15
字体:
来源:转载
供稿:网友


  1public class cryptutil
  2    {
  3        public static string decryptstring(string input)
  4        {
  5            if (input.equals(string.empty))
  6            {
  7                return input;
  8            }
  9
 10            byte[] bykey = {0x63, 0x68, 0x65, 0x6e, 0x79, 0x75, 0x61, 0x6e};
 11            byte[] iv = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
 12            byte[] inputbytearray = new byte[input.length];
 13            descryptoserviceprovider des = new descryptoserviceprovider();
 14            inputbytearray = convert.frombase64string(input);
 15            memorystream ms = new memorystream();
 16            cryptostream cs = new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write);
 17            cs.write(inputbytearray, 0, inputbytearray.length);
 18            cs.flushfinalblock();
 19            encoding encoding = new utf8encoding();
 20            return encoding.getstring(ms.toarray());
 21        }
 22
 23        public static string encryptstring(string input)
 24        {
 25            if (input.equals(string.empty))
 26            {
 27                return input;
 28            }
 29
 30            byte[] bykey = {0x63, 0x68, 0x65, 0x6e, 0x79, 0x75, 0x61, 0x6e};
 31            byte[] iv = {0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
 32            descryptoserviceprovider des = new descryptoserviceprovider();
 33            byte[] inputbytearray = encoding.utf8.getbytes(input);
 34            memorystream ms = new memorystream();
 35            cryptostream cs = new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write);
 36            cs.write(inputbytearray, 0, inputbytearray.length);
 37            cs.flushfinalblock();
 38            return convert.tobase64string(ms.toarray());
 39        }
 40        /**//// <summary>
 41        /// des + base64 加密
 42        /// </summary>
 43        /// <param name="input">明文字符串</param>
 44        /// <returns>已加密字符串</returns>
 45        public static string desbase64encrypt(string input)
 46        {
 47            system.security.cryptography.des des = system.security.cryptography.des.create();
 48            des.mode = system.security.cryptography.ciphermode.ecb;
 49            icryptotransform ct;
 50            memorystream ms;
 51            cryptostream cs;
 52            byte[] byt;
 53            byte[] key = new byte[8]{56,50,55,56,56,55,49,49};
 54            byte[] iv = new byte[8]{0,0,0,0,0,0,0,0};
 55
 56            ct = des.createencryptor(key, iv);
 57
 58            byt = encoding.getencoding("gb2312").getbytes(input); //根据 gb2312 编码对字符串处理,转换成 byte 数组
 59           
 60            ms = new memorystream();
 61            cs = new cryptostream(ms, ct, cryptostreammode.write);
 62            cs.write(byt, 0, byt.length);
 63            cs.flushfinalblock();
 64
 65            cs.close();
 66
 67            byte[] answer = ms.toarray();
 68            for(int j=0;j<answer.length;j++)
 69            {
 70                console.write(answer[j].tostring()+ " ");
 71            }
 72            console.writeline();
 73            return convert.tobase64string(ms.toarray()); // 将加密的 byte 数组依照 base64 编码转换成字符串
 74        }
 75
 76        /**//// <summary>
 77        /// des + base64 解密
 78        /// </summary>
 79        /// <param name="input">密文字符串</param>
 80        /// <returns>解密字符串</returns>
 81        public static string desbase64decrypt(string input)
 82        {
 83            system.security.cryptography.des des = system.security.cryptography.des.create();
 84            des.mode = system.security.cryptography.ciphermode.ecb;
 85            icryptotransform ct;
 86            memorystream ms;
 87            cryptostream cs;
 88            byte[] byt;
 89            byte[] key = new byte[8]{56,50,55,56,56,55,49,49};
 90            byte[] iv = new byte[8]{0,0,0,0,0,0,0,0};
 91           
 92            ct = des.createdecryptor(key, iv);
 93            byt = convert.frombase64string(input); // 将 密文 以 base64 编码转换成 byte 数组
 94
 95            ms = new memorystream();
 96            cs = new cryptostream(ms, ct, cryptostreammode.write);
 97            cs.write(byt, 0, byt.length);
 98            cs.flushfinalblock();
 99
100            cs.close();
101
102            return encoding.getencoding("gb2312").getstring(ms.toarray()); // 将 明文 以 gb2312 编码转换成字符串
103        }
104       
105       
106       
107        /**//// <summary>
108        /// des + base64 加密
109        /// </summary>
110        /// <param name="input">明文字符串</param>
111        /// <returns>已加密字符串</returns>
112        public static string desbase64encryptforid5(string input)
113        {
114            system.security.cryptography.des des = system.security.cryptography.des.create();
115            des.mode = system.security.cryptography.ciphermode.cbc;
116            icryptotransform ct;
117            memorystream ms;
118            cryptostream cs;
119            byte[] byt;
120            byte[] key = new byte[8]{56,50,55,56,56,55,49,49};
121            byte[] iv = new byte[8]{56,50,55,56,56,55,49,49};
122
123            ct = des.createencryptor(key, iv);
124
125            byt = encoding.getencoding("gb2312").getbytes(input); //根据 gb2312 编码对字符串处理,转换成 byte 数组
126           
127            ms = new memorystream();
128            cs = new cryptostream(ms, ct, cryptostreammode.write);
129            cs.write(byt, 0, byt.length);
130            cs.flushfinalblock();
131
132            cs.close();
133
134            byte[] answer = ms.toarray();
135            for(int j=0;j<answer.length;j++)
136            {
137                console.write(answer[j].tostring()+ " ");
138            }
139            console.writeline();
140            return convert.tobase64string(ms.toarray()); // 将加密的 byte 数组依照 base64 编码转换成字符串
141        }
142       
143       
144        /**//// <summary>
145        /// des + base64 解密
146        /// </summary>
147        /// <param name="input">密文字符串</param>
148        /// <returns>解密字符串</returns>
149        public static string desbase64decryptforid5(string input)
150        {
151            system.security.cryptography.des des = system.security.cryptography.des.create();
152            des.mode = system.security.cryptography.ciphermode.cbc;
153            icryptotransform ct;
154            memorystream ms;
155            cryptostream cs;
156            byte[] byt;
157            byte[] key = new byte[8]{56,50,55,56,56,55,49,49};
158            byte[] iv = new byte[8]{56,50,55,56,56,55,49,49};
159           
160            ct = des.createdecryptor(key, iv);
161            byt = convert.frombase64string(input); // 将 密文 以 base64 编码转换成 byte 数组
162
163            ms = new memorystream();
164            cs = new cryptostream(ms, ct, cryptostreammode.write);
165            cs.write(byt, 0, byt.length);
166            cs.flushfinalblock();
167
168            cs.close();
169
170            return encoding.getencoding("gb2312").getstring(ms.toarray()); // 将 明文 以 gb2312 编码转换成字符串
171        }
172       
173
174        /**//// <summary>
175        /// 3des 加密 byte[] to hex string
176        /// </summary>
177        /// <param name="input">明文字符串</param>
178        /// <returns>已加密字符串</returns>
179        public static string threedesencrypthex(string input)
180        {
181            string result = "";
182            system.security.cryptography.tripledes des = system.security.cryptography.tripledes.create();
183            des.mode = system.security.cryptography.ciphermode.cbc;
184            des.padding = system.security.cryptography.paddingmode.pkcs7;
185            icryptotransform ct;
186            memorystream ms;
187            cryptostream cs;
188            byte[] byt;
189            byte[] key = new byte[24]{
190                                         1,2,3,4,5,6,
191                                         1,2,3,4,5,6,
192                                         1,2,3,4,5,6,
193                                         1,2,3,4,5,6
194                                     };
195            byte[] iv = new byte[8]{1,2,3,4,5,6,1,2};
196
197            ct = des.createencryptor(key, iv);
198
199            byt = encoding.getencoding("gb2312").getbytes(input); //根据 gb2312 编码对字符串处理,转换成 byte 数组
200           
201            ms = new memorystream();
202            cs = new cryptostream(ms, ct, cryptostreammode.write);
203            cs.write(byt, 0, byt.length);
204            cs.flushfinalblock();
205
206            cs.close();
207
208            byte[] answer = ms.toarray();
209            for(int j=0;j<answer.length;j++)
210            {
211                result += answer[j].tostring("x").padleft(2,'0');
212            }
213            return result;
214        }
215
216        /**//// <summary>
217        /// 3des + hex to byte[] 解密
218        /// </summary>
219        /// <param name="input">密文字符串</param>
220        /// <returns>解密字符串</returns>
221        public static string threedesdecrypthex(string input)
222        {
223            system.security.cryptography.tripledes des = system.security.cryptography.tripledes.create();
224            des.mode = system.security.cryptography.ciphermode.cbc;
225            des.padding = system.security.cryptography.paddingmode.pkcs7;
226            icryptotransform ct;
227            memorystream ms;
228            cryptostream cs;
229            byte[] key = new byte[24]{
230                                         1,2,3,4,5,6,
231                                         1,2,3,4,5,6,
232                                         1,2,3,4,5,6,
233                                         1,2,3,4,5,6
234                                     };
235            byte[] iv = new byte[8]{1,2,3,4,5,6,1,2};
236           
237            ct = des.createdecryptor(key, iv);
238            //byt = convert.frombase64string(input); // 将 密文 以 hex to byte[]编码转换成 byte 数组
239            if(input.length<=1)
240            {
241                throw new exception("encrypted hex string is too short!");
242            }
243            byte[] byt = new byte[input.length/2];
244            for(int i=0;i<byt.length;i++)
245            {
246                //console.writeline(input.substring(i*2,2));
247                byt[i] = convert.tobyte(input.substring(i*2,2),16);
248            }
249
250            ms = new memorystream();
251            cs = new cryptostream(ms, ct, cryptostreammode.write);
252            cs.write(byt, 0, byt.length);
253            cs.flushfinalblock();
254
255            cs.close();
256
257            return encoding.getencoding("gb2312").getstring(ms.toarray()); // 将 明文 以 gb2312 编码转换成字符串
258        }
259        /**//// <summary>
260        /// base64解码
261        /// </summary>
262        /// <param name="base64str"></param>
263        /// <returns></returns>
264        public static string decodingfrombase64(string base64str)
265        {
266            byte[] bytes = convert.frombase64string(base64str);
267            return system.text.encoding.utf8.getstring(bytes);
268        }
269        /**//// <summary>
270        /// base64编码
271        /// </summary>
272        /// <param name="str"></param>
273        /// <returns></returns>
274        public static string encodingtobase64(string str)
275        {
276            return convert.tobase64string(encoding.utf8.getbytes(str));
277        }
278        /**//// <summary>
279        /// 根据指定的编码方式base64解码
280        /// </summary>
281        /// <param name="base64str"></param>
282        /// <param name="strencoding"></param>
283        /// <returns></returns>
284        public static string decodingfrombase64(string base64str,system.text.encoding strencoding)
285        {
286            byte[] bytes = convert.frombase64string(base64str);
287            return strencoding.getstring(bytes);
288        }
289        /**//// <summary>
290        /// 根据指定的编码方式base64编码
291        /// </summary>
292        /// <param name="str"></param>
293        /// <param name="strencoding"></param>
294        /// <returns></returns>
295        public static string encodingtobase64(string str,system.text.encoding strencoding)
296        {
297            return convert.tobase64string(strencoding.getbytes(str));
298        }
299    }
两个常用的方法

 1    /**//// <summary>
 2        /// 通过字节数组形式的密钥获取字符串形式的密钥
 3        /// </summary>
 4        void getstringbybytearray()
 5        {
 6            byte[] key = new byte[8]{56,50,55,56,56,55,49,49};
 7            response.write(system.text.encoding.default.getstring(key));
 8            response.end();
 9        }
10
11        /**//// <summary>
12        /// 通过字符串形式的密钥获取字节数组形式的密钥
13        /// </summary>
14        void getbytearraybystring()
15        {
16            string key = "82788711";
17            response.write(system.text.encoding.default.getbytes(key));
18            response.end();
19           
20        }

有这里没包括的,欢迎回复,大家一起总结一下~~

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表