All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
如何在PHP程序中防止盗链_[PHP教程]
SQL的排序,分组,统计常用语句_[SQL Server教程]
理解XML数据库_[XML教程]
C#网络应用编程基础练习题与答案(二)_[Asp.Net教程]
asp.net新闻标题后加new图片_[Asp.Net教程]
技巧文集:php的mysql性能优化_php资料_编程技术
ASP.NET,MVC,Framework与WCSF中MVP模式之小小比较_[Asp.Net教程]
ASP.NET从零起步设计网站全过程(2)_[Asp.Net教程]
PHP开发技巧之用递归替换数组中的内容_php资料_编程技术
PHP技巧:PHP脚本中关于拼写检查函数库_php资料_编程技术
Reading number is top 10 pictures
大人物的礼物
Magnificent cloud1
Discharge accidentally Actresses by the breast1
Beautiful Japanese beauty(漂亮的日本美女)
狗狗与主人神同步1
水晶头骨造型的酒瓶
Flow chart of breast implants
住院一星期,检测费两万
No trading, no killing
非常漂亮的泳装美女
Download software ranking
中国结婚习俗实录
SP3 for SQL2000
Tram sex maniac 2 (H) rar bag14
Tram sex maniac 2 (H) rar bag7
Unix video tutorial14
WebService在.NET中的实战应用教学视频 → 第1集
WebService在.NET中的实战应用教学视频 → 第4集
Boxer Classic video1
网页特效实例大全
天龙八部十二宫服务端
aaa published in(发表于) 2013/12/15 8:42:33 Edit(编辑)
DotNet中用到的加密算法总结_.net资料_编程技术

DotNet中用到的加密算法总结_.net资料_编程技术

DotNet中用到的加密算法总结_.net资料_编程技术-你的首页-uuhomepage.com

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 /**////


41 /// DES + Base64 加密
42 ///

43 /// 明文字符串
44 /// 已加密字符串
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 69 {
70 Console.Write(answer[j].ToString()+ " ");
71 }
72 Console.WriteLine();
73 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
74 }
75
76 /**////
77 /// DES + Base64 解密
78 ///

79 /// 密文字符串
80 /// 解密字符串
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 /**////
108 /// DES + Base64 加密
109 ///

110 /// 明文字符串
111 /// 已加密字符串
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;j136 {
137 Console.Write(answer[j].ToString()+ " ");
138 }
139 Console.WriteLine();
140 return Convert.ToBase64String(ms.ToArray()); // 将加密的 byte 数组依照 Base64 编码转换成字符串
141 }
142
143
144 /**////
145 /// DES + Base64 解密
146 ///

147 /// 密文字符串
148 /// 解密字符串
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 /**////
175 /// 3DES 加密 Byte[] to HEX string
176 ///

177 /// 明文字符串
178 /// 已加密字符串
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;j210 {
211 result += answer[j].ToString("x").PadLeft(2,'0');
212 }
213 return result;
214 }
215
216 /**////
217 /// 3DES + HEX to byte[] 解密
218 ///

219 /// 密文字符串
220 /// 解密字符串
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;i245 {
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 /**////
260 /// Base64解码
261 ///

262 ///
263 ///
264 public static string DecodingFromBase64(string base64Str)
265 {
266 Byte[] bytes = Convert.FromBase64String(base64Str);
267 return System.Text.Encoding.UTF8.GetString(bytes);
268 }
269 /**////
270 /// Base64编码
271 ///

272 ///
273 ///
274 public static string EncodingToBase64(string str)
275 {
276 return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
277 }
278 /**////
279 /// 根据指定的编码方式Base64解码
280 ///

281 ///
282 ///
283 ///
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 /**////
290 /// 根据指定的编码方式Base64编码
291 ///

292 ///
293 ///
294 ///
295 public static string EncodingToBase64(string str,System.Text.Encoding strEncoding)
296 {
297 return Convert.ToBase64String(strEncoding.GetBytes(str));
298 }
299 }
两个常用的方法


1 /**////


2 /// 通过字节数组形式的密钥获取字符串形式的密钥
3 ///

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 /**////
12 /// 通过字符串形式的密钥获取字节数组形式的密钥
13 ///

14 void GetByteArrayByString()
15 {
16 string key = "82788711";
17 Response.Write(System.Text.Encoding.Default.GetBytes(key));
18 Response.End();
19
20 }


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





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.