C#雜湊Md5加密

來源:互聯網
上載者:User
來自:http://hi.baidu.com/zhouwillpower/blog/item/e8da888ba81060d4fd1f1052.html
public partial class HashEncrypt
    {
        /// <summary>
        /// </summary>
        /// <param name="strSource">待加密字串</param>
        /// <returns>加密後的字串</returns>
        public static string MD5Encrypt(string strSource)
        {
            return MD5Encrypt(strSource, 16);
        }

        /// <summary>
        /// </summary>
        /// <param name="strSource">待加密字串</param>
        /// <param name="length">16或32值之一,其它則採用.net預設MD5密碼編譯演算法</param>
        /// <returns>加密後的字串</returns>
        public static string MD5Encrypt(string strSource, int length)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(strSource);
            byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
            StringBuilder sb = new StringBuilder();
            switch (length)
            {
                case 16:
                    for (int i = 4; i < 12; i++)
                        sb.Append(hashValue[i].ToString("x2"));
                    break;
                case 32:
                    for (int i = 0; i < 16; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
                default:
                    for (int i = 0; i < hashValue.Length; i++)
                    {
                        sb.Append(hashValue[i].ToString("x2"));
                    }
                    break;
            }
            return sb.ToString();
        }

        /// <summary>
        /// md5 encrypt
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MSMD5(String str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(str);
            byte[] result = md5.ComputeHash(data);
            String ret = "";
            for (int i = 0; i < result.Length; i++)
                ret += result[i].ToString("x").PadLeft(2, '0');
            return ret;
        }

        /// <summary>
        /// get md5 hash value
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        static string MSGetMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();
            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        /// <summary>
        /// Verify a hash against a string.
        /// </summary>
        /// <param name="input"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        static bool VerifyMd5Hash(string input, string hash)
        {
            // Hash the input.
            string hashOfInput = MSGetMd5Hash(input);
            // Create a StringComparer an comare the hashes.
            StringComparer comparer = StringComparer.OrdinalIgnoreCase;
            if (0 == comparer.Compare(hashOfInput, hash))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

MD5是單向加密的,嗯 ,可逆加密怎麼做呢?嗯 ,這裡收藏一個不錯的文章,總結得挺全。共兩部分,感謝興趣的自己看吧。

http://hi.baidu.com/srxljl/blog/item/a0508cb130cbe6550823020e.html 嗯 ,急著用的話,可以看這裡,這個簡單好懂 http://hi.baidu.com/planner/blog/item/04f3272d10b41a30359bf729.html http://hi.baidu.com/flying02/blog/item/94f185448ebd4c4c500ffe6c.html

private string MD5(string str)
        {
            string cl = str;
            string pwd = "";
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();//執行個體化一個md5對像 
            // 加密後是一個位元組類型的數組,這裡要注意編碼UTF8/Unicode等的選擇  
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通過使用迴圈,將位元組類型的數群組轉換為字串,此字串是常規字元格式設定化所得 
            for (int i = 0; i < s.Length; i++)
            {
                // 將得到的字串使用十六進位類型格式。格式後的字元是小寫字母,如果使用大寫(X)則格式後的字元是大寫字元  
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.