問(wèn)題重現(xiàn)
這個(gè) API 是當(dāng)時(shí)給 Lyra 應(yīng)用做激活用的,遂打開(kāi) Lyra 試了下,卻發(fā)現(xiàn)一切正常,于是可以排除服務(wù)端的問(wèn)題
放出導(dǎo)致錯(cuò)誤的源碼(來(lái)自 MSDN):
public string CalculateMD5Hash(string input){ // step 1, calculate MD5 hash from input MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hash = md5.ComputeHash(inputBytes); // step 2, convert byte array to hex string StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString(“X2”)); } return sb.ToString();}實(shí)質(zhì)
MD5 有很多版本,其實(shí)這段代碼并沒(méi)有錯(cuò),但是 php 的 md5 函數(shù)默認(rèn)返回的是 32位小寫(xiě) ,而以上這一段返回的是 16位小寫(xiě)
于是想辦法把這個(gè) func 改為 32位小寫(xiě)輸出即可
public static String md5(String s){ MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s); bytes = md5.ComputeHash(bytes); md5.Clear(); string ret = ""; for (int i = 0; i < bytes.Length; i++) { ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0'); } return ret.PadLeft(32, '0');}總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注