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(, , , , , , (