Completely solve the problem that ASP. NET MD5 encrypts Chinese characters and ASP is inconsistent

Source: Internet
Author: User

When the string to be MD5 encrypted does not contain Chinese characters, the encryption result of ASP. NET is the same as that of ASP:

 1 response. Write (formsauthentication. hashpasswordforstoringinconfigfile ("www.mzwu.com", "MD5 "));
2 // result: d66e1f138689b9b5aa4c520d9eaffb61

 

 Response. Write (MD5 ("www.mzwu.com", 32 ))
'Result: d66e1f138689b9b5aa4c520d9eaffb61

When the MD5 encrypted string contains Chinese characters, the encryption results are inconsistent:

 Response. Write (formsauthentication. hashpasswordforstoringinconfigfile ("wooden house", "MD5 "));
// Result: 34d9cbd5164c47058dfa3af832e2d1dc

 

 Response. Write (MD5 ("wooden house", 32 ))
'Result: 0a40a901_da023ae7aa17771663a41e

 

We know that ASP. NET uses the UTF-8 encoding format by default, while ASP uses the gb2312 encoding format, which leads to different Chinese encryption results. Let's see how to make ASP. the encoding result of net is the same as that of ASP. net adopts the gb2312 encoding format, which is formsauthentication. the hashpasswordforstoringinconfigfile () method cannot be implemented. We need to use system. security. cryptography. the computehash method of the md5cryptoserviceprovider object to encrypt: S

Code

 1 md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
2 response. Write (bitconverter. tostring (md5.computehash (encoding. getencoding ("gb2312"). getbytes (""). Replace ("-",""));
3 // result: 0a40a901_da023ae7aa17771663a41e

It is easy to use UTF-8 encryption:

Code

 1 md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
2 response. Write (bitconverter. tostring (md5.computehash (encoding. getencoding ("UTF-8"). getbytes (""). Replace ("-",""));
3 // result: 34d9cbd5164c47058dfa3af832e2d1dc

The problem seems to be a perfect solution. Let's make it better: When the string to be encrypted is transmitted from other pages, the encoding format for other pages may be gb2312 or UTF-8, there may be other encoding formats. How can this problem be solved? You may think it is very easy to use its previous encoding format for encryption? In the actual test, you will find two very serious problems:

1. We do not know the encoding format used when the parameter is passed over;

2. If the two pages use different encoding methods, the parameter values received by the request will be garbled, so do not talk about encryption;

Problem 1 is better solved, requiring the other party to add a parameter description to the encoding format while passing the parameter. Problem 2 is to directly receive the parameter value without using the request, let's talk about the following functions:

Code

 1/** // <summary>
2 // perform MD5 encryption on the string
3 /// </Summary>
4 /// <Param name = "text"> string to be encrypted </param>
5 // <Param name = "charset"> string encoding format </param>
6 /// <example> STR = MD5 ("wooden house", "gb2312"); </example>
7 /// <returns> </returns>
8 Public String MD5 (string text, string charset)
9 {
10 return (MD5 (text, charset, false ));
11}
12
13/** // <summary>
14 // perform MD5 encryption on the string or parameter value
15 /// </Summary>
16 /// <Param name = "text"> name of the string or parameter to be encrypted </param>
17 /// <Param name = "charset"> string encoding format </param>
18 /// <Param name = "isarg"> encrypted string type true: parameter value false: String </param>
19 /// <returns> </returns>
20 Public String MD5 (string text, string charset, bool isarg)
21 {
22 try
23 {
24 md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();
25
26 if (isarg)
27 {
28 namevaluecollection collect = httputility. parsequerystring (request. url. query, encoding. getencoding (charset); // use collect to receive parameter values
29 If (collect [text]! = NULL)
30 {
31 return bitconverter. tostring (md5.computehash (encoding. getencoding (charset). getbytes (collect [text]. tostring (). Replace ("-","");
32}
33}
34 else
35 {
36 return bitconverter. tostring (md5.computehash (encoding. getencoding (charset). getbytes (text). Replace ("-","");
37}
38}
39 catch {}
40
41 return string. empty;
42}

Note 1: namespace to be introduced in the above Code

 using System.Text;
using System.Web.Security;
using System.Security.Cryptography;
using System.Collections.Specialized;

How do I convert the 2nd 32-bit ciphertext into 16-bit ciphertext?

The 16-bit ciphertext is a string of 9 to 24 characters including 32-bit ciphertext. For example, "0a40a901_da023ae7aa17771663a41e" → "90da023ae7aa1777"

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.