Cuyahoga研究之 利用MD5CryptoServiceProvider返回字串的MD5 Hash值

來源:互聯網
上載者:User
 

Encryption.cs檔案是該項目中用於進行加密的部分
類Encryption只有一個靜態方法StringToMD5Hash(string inputString),代碼如下:

public static string StringToMD5Hash(string inputString)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] encryptedBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(inputString));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString();
        }

加密的過程很簡單,不過,這個算是一個常式吧,你甚至可以搬過去就用
Encoding.ASCII.GetBytes(inputString) 用於以ASCII方式將一個字串轉換成一個位元組數組,原因是ComputeHash方法只接收Byte[]參數,後面的內容就是將加密後的Byte[] 連成一個字串,AppendFormat中的格式字串{0:x2}是指將數組中每一個字元格式設定化為十六進位,精度為2

嗯,其實有些東西很簡單,不過,沒做過時,卻不知從何開始,不過,看別人的代碼的確能找到不少答案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.