How to add an MD5 password in Asp.net

Source: Internet
Author: User
Tags md5 encryption sha1 encryption microsoft address
<% @ Page Language = "C #" contenttype = "text/html" %>
<% @ Import namespace = "system" %>
<Script language = "C #" runat = "server">
Void page_load (Object sender, eventargs e ){
// Obtain the field to be encrypted and convert it to a byte [] Array
Byte [] DATA = system. Text. encoding. Unicode. getbytes (source. Text. tochararray ());
// Create an encryption service
System. Security. cryptography. MD5 MD5 = new system. Security. cryptography. md5cryptoserviceprovider ();
// Encrypt byte [] Arrays
Byte [] result = md5.computehash (data );
// Convert the encrypted array into a field
String sresult = system. Text. encoding. Unicode. getstring (result );
// Display
Sha1_1.text = "MD5 common encryption:" + sresult. tostring () + "<br/> ";
// Encrypted as a password
String enpswdstr = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (source. Text. tostring (), "MD5 ");
// Display
Sha1_2.text = "MD5 password encryption:" + enpswdstr + "<br/> ";
}
</SCRIPT>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test </title>
</Head>
<Body>
<H3> sha1 encryption <Form runat = "server">
<Asp: Label id = "sha1_1" runat = "server"> </ASP: Label>
<Asp: Label id = "sha1_2" runat = "server"> </ASP: Label>
<Asp: textbox id = "Source" runat = "server" textmode = "singleline" text = "test" autopostback = "true"/>
(Press ENTER)
</Form>
</Body>
</Html> ========================================== ============================================== This is a transfer to others, and the source is unknown.
ASP. NET implements MD5 Encryption Algorithm

In fact, there is a simple MD5 Implementation Method in. net.

/// MD5 Encryption
///
///Encrypted string /// Encrypted string
Public static string MD5 (string tocrystring)
{
Return formsauthentication. hashpasswordforstoringinconfigfile (tocrystring, "MD5 ");
}

Public static string MD5 (string Str)
{
MD5 MD5 = new md5cryptoserviceprovider ();
Byte [] result = md5.computehash (system. Text. encoding. Default. getbytes (STR ));
String str2 = "";
For (INT I = 0; I {
Str2 + = string. Format ("{0: x}", result [I]);
}
Return str2;
}

///

/// Perform MD5 encoding on the string */
/// Return value: the encoded MD5 password string
/// Input: Pre-encrypted original string
///
//////
Public static string sysencode (string xstr)
{
String STR = "";
Try
{
// Add the pz id before the string
Xstr = "PZ" + xstr;
STR = system. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (xstr, "MD5"). tostring ();

}
Catch
{
STR = ""; // Error
}
Return STR; // return the encrypted string
}

Settlement of the encrypted results, write Java, PHP, ASP and other standard algorithms are different!
In fact, there is a simple MD5 Implementation Method in. net.
/**////

/// MD5 Encryption
///
///Encrypted string /// Encrypted string
Public static string MD5 (string tocrystring)
{
Return formsauthentication. hashpasswordforstoringinconfigfile (tocrystring, "MD5 ");
}
So how to convert the standard conversion from byte to string? I have read that Quickstart uses
Bitconverter. tostring
In this way, the standard MD5 format should be written!
Public static string MD5 (string tocrystring)
{
Md5cryptoserviceprovider hashmd5;
Hashmd5 = new md5cryptoserviceprovider ();
Return bitconverter. tostring (hashmd5.computehash (encoding. default. getbytes (tocrystring ))). replace ("-",""). tolower (); // ASP is in lower case and all characters are reduced to write
}
The encrypted content is the same as the standard content!

I have solved this problem. I posted a post before the problem occurred on the csdn server.

Because the MD5 Algorithm Used in general ASP is a VBScript algorithm written by a foreigner. It uses a 16-bit algorithm internally, and the. NET algorithm is inherently 32-bit.

The above explanation is incorrect, but it can be determined that the MD5 Algorithm in ASP is incompatible with the built-in Algorithm in. net. There is no way to make the results consistent.

There is no alternative. On the MS Platform, all scripting languages, including VBScript, JavaScript, and JScript, use the same script processor named scripthost, search for script56 on the official Microsoft download site. For this scripthost, Microsoft released a COM component, which is specially handled and can be called and controlled by the. NET Runtime Library.

With the above method, you can call scripthost by installing this controller on the Asp.net site.

The call process is, first in the http://www.microsoft.com/downloads/details.aspx? Displaylang = ZH-CN & familyid = download microsoft's official scripthostcontrol control from the D05FCF37-4D9F-4769-9442-0BCEEF907033, install it, and install it. Open vs.net, select the "Reference" node of the project in Solution Explorer of the project, select Add reference, and locate the msscript. ocx file in the directory selected during installation.

Then calculate the MD5 Algorithm in ASP.Code(Note: Response and request-specific ASP objects cannot be included) copied to a vbs file. Write code in vs.net using the streamreader class to read the vbs file into a string variable, then call the referenced object to control scripthost, assign the vbs code to this host, and then read the result, you can get the same result as ASP.

I have written the sample document, including an MD5 Algorithm VBScript file under ASP, an Asp.net page, and a relative. CS file. The compressed package also contains the scriptcontrol ActiveX control. If you don't trust me, you can download it from the Microsoft address above.
Sample download in: http://submaie.aspsir.com/temp/CSharpRunScript.rar

MD5 encryption in ASP is the same as the algorithm result in ASP. NET for English letters or numbers (single-byte.

It is pointed out that MD5 in ASP is usually in lower case and. NET is in upper case.

If your asp encryption result is 16 bits, it is estimated that the MD5 class of the dynamic network forum you are using. Only the 32-bit MD5 center and 16-bit are available for the Internet forum. At this time, you can change the MD5 class of ASP (just the last sentence "MD5 = lcase (wordtohex (B) & wordtohex (c)" to "lcase (wordtohex (a) & wordtohex (B) & wordtohex (c) & wordtohex (D ))"). The result in implementing ASP is also 32 bits. Or, only the middle 16 bits are used to obtain the. NET encryption result.

If the problem is caused by dubyte. I'm afraid there is no good solution. Single-byte MD5 is the same regardless of. Net/Java/asp/PHP.

From: http://blog.csdn.net/JOHNCOOLS/archive/2006/03/29/643301.aspx

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.