C # compatible with MD5 in PHP

Source: Internet
Author: User

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

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.