源文件下载 md5.rarCode
1using System.Security.Cryptography;
2using System.IO;
3using System.Text;
4
5
6/**//*
7 * 本类功能:
8 * Md5加密
9 * 返回md5hash字符串
10 * 返回兼容asp 的 md5的值
11 * 16位为兼容asp加密类
12 * 32位为.net正常加密类
13 */
14public partial class MD5
15{
16 MD5#region MD5
17 /**//// <summary>
18 /// 16位MD5加密方法,以前的DVBBS所使用
19 /// </summary>
20 /// <param name="strSource">待加密字串</param>
21 /// <returns>加密后的字串</returns>
22 public static string MD5Encrypt(string strSource)
23 {
24 return MD5Encrypt(strSource, 16);
25 }
26 /**//// <summary>
27 /// MD5加密,和动网上的16/32位MD5加密结果相同
28 /// </summary>
29 /// <param name="strSource">待加密字串</param>
30 /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
31 /// <returns>加密后的字串</returns>
32 public static string MD5Encrypt(string strSource, int length)
33 {
34 byte[] bytes = Encoding.ASCII.GetBytes(strSource);
35 byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
36 StringBuilder sb = new StringBuilder();
37 switch (length)
38 {
39 case 16:
40 for (int i = 4; i < 12; i++)
41 sb.Append(hashValue[i].ToString("x2"));
42 break;
43 case 32:
44 for (int i = 0; i < 16; i++)
45 {
46 sb.Append(hashValue[i].ToString("x2"));
47 }
48 break;
49 default:
50 for (int i = 0; i < hashValue.Length; i++)
51 {
52 sb.Append(hashValue[i].ToString("x2"));
53 }
54 break;
55 }
56 return sb.ToString();
57 }
58 #endregion
59}
60