How to generate the same MD5 Hash Code as PHP in C #

Source: Internet
Author: User
Tags md5 hash

Recently, a C # transformation has been made to an existing system. The system was previously implemented using PHP, and the background administrator logged on using the MD5 encryption algorithm. In PHP, it is very easy to encrypt a string using MD5, just one line of code:

("Something you want to encrypt.")

Call the md5 () method directly, and then upload the MD5 encrypted string to obtain the returned hash code. There should also be corresponding algorithms in C! Right? First, I tried the following code. The resulting hash code is different from that of PHP.

   MD5( FormsAuthentication.HashPasswordForStoringInConfigFile(stringToHash, 

Therefore, we have to use the MD5CryptoServiceProvider object of C # To write code for conversion.

1. instantiate the MD5CryptoServiceProvider object

2. Convert the string into a byte array

3. Use the ComputeHash () method of the MD5CryptoServiceProvider object to encrypt the byte array and return the converted byte array.

4. Before converting a byte array to a string, you need to traverse it and convert it as follows:

myByte.ToString().ToLower()

Then, you can get the same MD5 hash code as in PHP. Why. NET is so troublesome, maybe this is one of the reasons why so many developers are still keen on PHP development. Every programming language has its own charm, it also has its meaning!

Based on the above discussion, the complete code is as follows:

   MD5ForPHP( md5 = [] emailBytes =[] hashedEmailBytes ==  ( b 

Alternatively, you can write the above method as a C # extension method. You only need to modify the method signature.

   MD5ForPHP( String, }

In many ways, the PHP program and the C # program involve conversions between formats. If the server running PHP is of the UNIX type, there will also be conversions between date formats. The following two methods demonstrate how to convert UNIX time to C # DateTime and how to convert C # DateTime to UNIX time.

  DateTime UnixTimeStampToDateTime(    DateTime dtDateTime =  DateTime(, , , , , ,   = (datetime -  DateTime(, , , , , ,  (

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.