"Go" How To make PHP MD5 and C # MD5 consistent?

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.