In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to generate the same MD5 Hash Code method as PHP in C #". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Recently, a C # transformation is being carried out on an existing system, which used to be done with PHP, and the backstage administrator logged in using the MD5 encryption algorithm. In PHP, it is very simple to encrypt a string with MD5, with one line of code:
The copy code is as follows:
Md5 ("Something you want to encrypt.")
You can get the returned hash code by directly calling the md5 () method, and then passing in the string that you want to encrypt with MD5. There should also be a corresponding algorithm in C#! Isn't that right? I first tried the following code, and the result is that the hash code is not the same as PHP.
The copy code is as follows:
Public static string MD5 (string stringToHash)
{
Return FormsAuthentication.HashPasswordForStoringInConfigFile (stringToHash, "md5")
}
Therefore, we have to borrow the MD5CryptoServiceProvider object of C# to write our own code for conversion.
1. Instantiate MD5CryptoServiceProvider object
two。 Convert a string to an byte array
3. Use the ComputeHash () method of the MD5CryptoServiceProvider object to encrypt the byte array and return the converted byte array
4. Before you can convert the byte array to a string, you also 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 is it so troublesome in .NET? maybe this is one of the reasons why so many developers are still keen on PHP development. Each programming language has its own charm and meaning!
Based on the above discussion, the complete code is as follows:
The copy 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, just change the method signature.
The copy code is as follows:
Public static string MD5ForPHP (this String, string stringToHash)
{
/ / Your code here.
}
PHP programs and C # programs involve conversion between formats in many ways, and if the server running PHP is of type UNIX, there will also be conversion between date formats. The following two methods show how to convert UNIX time to C # DateTime and how to convert C # DateTime to UNIX time.
The copy 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,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
}
This is the end of the content of "how to generate the same MD5 Hash Code as PHP in C #". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.