Solution for differences between MD5 encryption in. NET and MD5 encryption value in ASP

Source: Internet
Author: User

Problem: an ASP to ASP is being created. net mall revision, the original registered users need to retain, the original user password is ASP MD5, 32-bit encryption, remember ASP MD5 and ASP.. Net's MD5 encryption results are not equal, so I tried:
The original MD5 encryption method is as follows:
/// <Summary>
/// MD5 Encryption Algorithm
/// </Summary>
/// <Param name = "md5str"> string to be encrypted </param>
/// <Returns> MD5 encryption result </returns>
Public static string getmd5 (string md5str)
{
Byte [] data;
String ret = "";
Data = system. Text. encoding. utf8.getbytes (md5str );
Md5cryptoserviceprovider md5obj = new md5cryptoserviceprovider ();
Byte [] md5byte = md5obj. computehash (data );
For (INT I = 0; I <md5byte. length; I ++)
{
RET + = md5byte [I]. tostring ("X"). padleft (2, '0 ');
}
Return ret;
}

Later, I found a simpler implementation method.
/// <Summary>
/// MD5 Encryption Algorithm
/// </Summary>
/// <Param name = "md5str"> string to be encrypted </param>
/// <Returns> MD5 encryption result </returns>
Public static string getmd5 (string md5str)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (md5str, "MD5 ");
}

Use the second method to output the MD5 encryption result of "admin:
21232F297A57A5A743894A0E4A801FC3
Comparison with ASP MD5-encrypted 16-bit and 32-bit encryption:
16-bit encryption (7a57a5a743894a0e)
32-bit encryption (21232f297a57a5a743894a0e4a801fc3)

It is found that the results are the same as those of 32-bit encryption, but the uppercase and lowercase letters are different. The 16-bit encryption results only include 16 in the middle of the 32-bit encryption results, so:
/// <Summary>
/// ASP MD5 Encryption Algorithm
/// </Summary>
/// <Param name = "md5str"> string to be encrypted </param>
/// <Param name = "type"> 16 or 32-bit encryption </param>
/// <Returns> Asp md5 encryption result </returns>
Public static string GetAspMd5 (string md5str, int type)
{
If (type = 16)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (md5str, "MD5"). substring (8, 16). tolower ();
}
Else if (type = 32)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (md5str, "MD5"). tolower ();
}
Return "";
}
The above method implements ASP MD5 encryption results in. NET.

----------------------- 2007.08.07 plus ----------------------------------

It seems that this problem has been solved for a long time. I found the following methods in my previous projects:

Public static string GetMD5 (string s)
{

/// <Summary>
/// ASP-compatible MD5 Encryption Algorithm
/// </Summary>

MD5 md5 = new MD5CryptoServiceProvider ();
Byte [] t = md5.ComputeHash (Encoding. GetEncoding ("gb2312"). GetBytes (s ));
Stringbuilder sb = new stringbuilder (32 );
For (INT I = 0; I <t. length; I ++)
{
SB. append (T [I]. tostring ("X"). padleft (2, '0 '));
}
Return sb. tostring ();
}

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.