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.
< Pre style= "MARGIN-TOP:1EM; Margin-right:1em; margin-bottom:0px; Margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; Overflow-x: auto; Overflow-y: auto; Color: #555555; ">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; }
}
C # compatible with MD5 in PHP