There is the code for C # generation MD5 as follows:
ClassCreateMD5 {StaticvoidMain(String[] (args) {String Source = "question Guide"; using (MD5 Md5hash = MD5. Create ()) {string hash = Getmd5hash (Md5hash, source); Console.WriteLine (hash); }} static string getmd5hash (MD5 Md5hash, string input) {//here is the Unicode byte[] data = Md5hash.computehash ( Encoding.Unicode.GetBytes (input)); StringBuilder Sbuilder = new StringBuilder (); for (int i = 0; i < data. Length; i++) {sbuilder.append (Data[i]. ToString ( "X2")); } return sbuilder.tostring ();}}
The MD5 generated by the above code is F5DA53705563C657581A6D0853286FDC now the problem is that C # generates MD5 inconsistent with the MD5 generated by PHP
Because of business constraints, you cannot change C # code, only from PHP.
Reply:
MD5 one step before the operation
$tmp = mb_convert_encoding(‘提问指南‘, ‘utf-16le‘, ‘utf8‘);
Original: C # compatible with PHP MD5
Due to work needs, C # needs to be used to do two development of a PHP program. A minor problem was found during login verification.
The result of the MD5 algorithm written in C # and PHP's MD5 () is sometimes different. The password verification of some accounts does not pass. Later on the internet to find a bit, in a foreign site found the answer.
Common MD5 algorithms for C #.
public static string MD5 (string password) { byte[] textbytes = System.Text.Encoding.Default.GetBytes (password); try { System.Security.Cryptography.MD5CryptoServiceProvider crypthandler; Crypthandler = new System.Security.Cryptography.MD5CryptoServiceProvider (); Byte[] hash = Crypthandler.computehash (textbytes); string ret = ""; foreach (Byte A in hash) { ret + = a.tostring ("x"); } return ret; } catch { throw; }
}
However, the result of this algorithm is different from that of MD5 (). After the adjustment is as follows, that is possible.
public static string MD5 (string password) { byte[] textbytes = System.Text.Encoding.Default.GetBytes (password); try { System.Security.Cryptography.MD5CryptoServiceProvider crypthandler; Crypthandler = new System.Security.Cryptography.MD5CryptoServiceProvider (); Byte[] hash = Crypthandler.computehash (textbytes); string ret = ""; foreach (Byte A in hash) { if (a<16) ret + = "0" + a.tostring ("x"); else ret + = a.tostring ("x"); } return ret; } catch { throw; }
}
"Go" How To make PHP MD5 and C # MD5 consistent?