System. Web. Security provides a very simple MD5 encryption algorithm, but how does one use MD5 encryption for non-web-layer projects?

Source: Internet
Author: User

I didn't care too much at first. I used this MD5 encryption method on the web layer.
Public static string MD5 (string Str)
{
Return formsauthentication. hashpasswordforstoringinconfigfile (STR, "MD5 ");
}

This MD5 encryption method is used on a non-web layer.
Public static string MD5 (string Str)
{
Byte [] bvalue;
Byte [] bhash;
String result = NULL;
Md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();

Bvalue = system. Text. encoding. utf8.getbytes (STR );

Bhash = md5.computehash (bvalue );

Md5.clear ();

Return convert. tobase64string (bhash );
}

But in fact the output results of the two methods are not the same. I checked it on msdn and didn't find anything wrong with me.
As a result, I have made some improvements to this method.

Public static string MD5 (string Str)
{
Byte [] bvalue;
Byte [] bhash;
String result = NULL;
Md5cryptoserviceprovider MD5 = new md5cryptoserviceprovider ();

Bvalue = system. Text. encoding. utf8.getbytes (STR );

Bhash = md5.computehash (bvalue );

Md5.clear ();

For (INT I = 0; I <bhash. length; I ++)
{
If (bhash [I]. tostring ("X"). Length = 1)
{
// If the returned value is a value like 07, 0 will be saved, so a 0 value is forcibly added.
Result + = "0" + bhash [I]. tostring ("X ");
}
Else
{
Result + = bhash [I]. tostring ("X ");
}

}
Return result. toupper ();
}

Successful :)
But I don't know whether I am smart, whether there is a coding problem or something else, but the result is the same.

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.