C#用MD5CryptoServiceProvider把字符串加密成32位Hash值


方法1:
    using System.Text;
    using System.Security.Cryptography;


        public string Hash(string toHash)
        {
            MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF7.GetBytes(toHash);
            bytes = crypto.ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            foreach (byte num in bytes)
            {
                sb.AppendFormat("{0:x2}", num);
            }
             return sb.ToString();        //32位
            return sb.ToString().Substring(8,16);        //16位
        }

 

方法2:

16位

   public string GetMd5(string str)
   {
    System.Security.Cryptography.MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
    string a=BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)),4,8);
    a=a.Replace("-","");
    return a;
   }

32位

   public string GetMd5(string str)
   {
    System.Security.Cryptography.MD5CryptoServiceProvider md5=new MD5CryptoServiceProvider();
    string a=BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)));
    a=a.Replace("-","");
    return a;
   }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM