C#MD5加密的两种方式
C#MD5加密的两种方式
在开发过程当中,我们经常会用到MD5加密,下面介绍MD5加密的两种方式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
/// <summary> /// MD5字符串加密 /// </summary> /// <param name="txt"></param> /// <returns>加密后字符串</returns> public static string GenerateMD5( string txt) { using (MD5 mi = MD5.Create()) { byte [] buffer = Encoding.Default.GetBytes(txt); //开始加密 byte [] newBuffer = mi.ComputeHash(buffer); StringBuilder sb = new StringBuilder(); for ( int i = 0; i < newBuffer.Length; i++) { sb.Append(newBuffer[i].ToString( "x2" )); } return sb.ToString(); } } /// <summary> /// MD5流加密 /// </summary> /// <param name="inputStream"></param> /// <returns></returns> public static string GenerateMD5(Stream inputStream) { using (MD5 mi = MD5.Create()) { //开始加密 byte [] newBuffer = mi.ComputeHash(inputStream); StringBuilder sb = new StringBuilder(); for ( int i = 0; i < newBuffer.Length; i++) { sb.Append(newBuffer[i].ToString( "x2" )); } return sb.ToString(); } } |
漫思