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:
Copy codeThe Code is as follows:
Md5 ("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.
Copy codeThe Code is as follows:
Public static string MD5 (string stringToHash)
{
Return FormsAuthentication. HashPasswordForStoringInConfigFile (stringToHash, "md5 ");
}
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 ("x2"). 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:
Copy codeThe Code is as follows:
Public static string MD5ForPHP (string stringToHash)
{
Var md5 = new System. Security. Cryptography. MD5CryptoServiceProvider ();
Byte [] emailBytes = Encoding. UTF8.GetBytes (stringToHash. ToLower ());
Byte [] hashedEmailBytes = md5.ComputeHash (emailBytes );
StringBuilder sb = new StringBuilder ();
Foreach (var B in hashedEmailBytes)
{
Sb. Append (B. ToString ("x2"). ToLower ());
}
Return sb. ToString ();
}
Alternatively, you can write the above method as a C # extension method. You only need to modify the method signature.
Copy codeThe Code is as follows:
Public static string MD5ForPHP (this String, string stringToHash)
{
// Your code here.
}
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.
Copy codeThe Code is as follows:
Public static DateTime UnixTimeStampToDateTime (long unixTimeStamp)
{
// Unix timestamp is seconds past epoch
DateTime dtDateTime = new DateTime (1970, 1, 1, 0, 0, 0, 0, System. DateTimeKind. Utc );
Return dtDateTime. AddSeconds (unixTimeStamp );
}
Public static long DateTimeToUnixTimeStamp (DateTime datetime)
{
TimeSpan span = (datetime-new DateTime (1970, 1, 1, 0, 0, 0, 0, DateTimeKind. Utc ));
Return (long) span. TotalSeconds;
}