asp.neti 加密三种方式
public string Get_MD5_Method1(string strSource) { System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //获取密文字节数组 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes (strSource)); //转换成字符串,并取9到25位 string strResult = BitConverter.ToString(bytResult, 4, 8); //转换成字符串,32位 //string strResult = BitConverter.ToString(bytResult); //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉 strResult = strResult.Replace("-", ""); return strResult; } --------------------------------------------第二种 public string Get_MD5_Method2(string strSource) { string strResult = ""; //Create System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create (); //注意编码UTF8、UTF7、Unicode等的选择 byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes (strSource)); //字节类型的数组转换为字符串 for (int i = 0; i < bytResult.Length; i++) { //16进制转换 strResult = strResult + bytResult[i].ToString("X"); } return strResult; } -------------------------简写-------------- public string Get_MD5_Method3(string strSource) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile (strSource, "MD5"); }
初入江湖程序猿