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"); 
} 

 

posted @ 2015-02-10 14:18  初入程序猿  阅读(229)  评论(0编辑  收藏  举报