源文件下载 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{
16MD5#region MD5
17/**//// <summary>
18/// 16位MD5加密方法,以前的DVBBS所使用
19/// </summary>
20/// <param name="strSource">待加密字串</param>
21/// <returns>加密后的字串</returns>
22public static string MD5Encrypt(string strSource)
23![]()
{
24return 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>
32public static string MD5Encrypt(string strSource, int length)
33![]()
{
34byte[] bytes = Encoding.ASCII.GetBytes(strSource);
35byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
36StringBuilder sb = new StringBuilder();
37switch (length)
38![]()
{
39case 16:
40for (int i = 4; i < 12; i++)
41sb.Append(hashValue[i].ToString("x2"));
42break;
43case 32:
44for (int i = 0; i < 16; i++)
45![]()
{
46sb.Append(hashValue[i].ToString("x2"));
47}
48break;
49default:
50for (int i = 0; i < hashValue.Length; i++)
51![]()
{
52sb.Append(hashValue[i].ToString("x2"));
53}
54break;
55}
56return sb.ToString();
57}
58#endregion
59}
60