在ASP.NET中MD5的加密方式很简单,代码如下:FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5").ToLower();需要注意的是,如果MD5加密的时候转换的是小写,那么在验证的时候也要转换成小写,保持统一。另外上述方式为32位的MD5加密方式,如果是16位的 则取32位加密结果的中间16位的值即可。 代码如下:////// MD5加密 /// /// 需要加密的明文 ///返回32位加密结果 public static string Get_MD5(string strSource, string sEncode) { //new System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //获取密文字节数组 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.GetEncoding(sEncode).GetBytes(strSource)); //转换成字符串,并取9到25位 //string strResult = BitConverter.ToString(bytResult, 4, 8); //转换成字符串,32位 string strResult = BitConverter.ToString(bytResult); //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉 strResult = strResult.Replace("-", ""); return strResult.ToLower(); }