md5加密

来源:互联网 发布:淘宝推广计划怎么写 编辑:程序博客网 时间:2024/06/05 05:12
  1. /// <summary>  
  2.         /// MD5加密,和动网上的16/32位MD5加密结果相同  
  3.         /// </summary>  
  4.         /// <param name="strSource">待加密字串</param>  
  5.         /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>  
  6.         /// <returns>加密后的字串</returns>  
  7.         public static string MD5Encrypt(string strSource, int length)  
  8.         {  
  9.             byte[] bytes = Encoding.ASCII.GetBytes(strSource);  
  10.             byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);  
  11.             StringBuilder sb = new StringBuilder();  
  12.             switch (length)  
  13.             {  
  14.                 case 16:  
  15.                     for (int i = 4; i < 12; i++)  
  16.                         sb.Append(hashValue[i].ToString("x2"));  
  17.                     break;  
  18.                 case 32:  
  19.                     for (int i = 0; i < 16; i++)  
  20.                     {  
  21.                         sb.Append(hashValue[i].ToString("x2"));  
  22.                     }  
  23.                     break;  
  24.                 default:  
  25.                     for (int i = 0; i < hashValue.Length; i++)  
  26.                     {  
  27.                         sb.Append(hashValue[i].ToString("x2"));  
  28.                     }  
  29.                     break;  
  30.             }  
  31.             return sb.ToString();  
  32.         }  
0 0
原创粉丝点击