Methods for implementing the MD5 Algorithm in. net

Source: Internet
Author: User
/// <Summary>  /// Obtain the MD5 value and convert it to base64  /// </Summary>  /// <Param name = "sourcein"> </param>  /// <Returns> </returns>  Public   Static   String MD5 ( String Sourcein)
{
System. Security. cryptography. md5cryptoserviceprovider md5csp = New System. Security. cryptography. md5cryptoserviceprovider ();
Byte [] Md5source = system. Text. encoding. utf8.getbytes (sourcein );
Byte [] Md5out = md5csp. computehash (md5source );

Return Convert. tobase64string (md5out );
}


/// <Summary> /// Obtain the MD5 value, which is not converted to base64 /// </Summary> /// <Param name = "sdatain"> string to be encrypted </param> /// <Param name = "move"> offset </param> /// <Returns> string encrypted by sdatain </returns> Public String Getmd5 ( String Sdatain, String Move)
{
System. Security. cryptography. md5cryptoserviceprovider MD5 = New System. Security. cryptography. md5cryptoserviceprovider ();
Byte [] BYT, bythash;
Byt = system. Text. encoding. utf8.getbytes (move + sdatain );
Bythash = md5.computehash (BYT );
Md5.clear ();
String Stemp =" ";
For ( Int I = 0; I <bythash. length; I ++)
{
Stemp + = bythash [I]. tostring (" X "). Padleft (2, '0 ');
}
Return Stemp;
}

/// <Summary> /// Obtain MD5 in Asp.net /// </Summary> /// <Param name = "Source"> </param> /// <Returns> </returns> Public String Md5aspdotnet ( String Source)
{
Return System. Web. Security. formsauthentication. hashpasswordforstoringinconfigfile (source ," MD5 ");
}
 
 
 
The results obtained by the first method are different from those obtained by the following methods, but the results are the same. If yourProgramIf it is transferred from ASP, 2nd or 3rd methods are recommended.

Extension Method:
Static class stringextensions
{
Public Static string nl2br (this string S)
{
Return S. Replace ("\ R \ n "," < BR /> "). Replace ("\ N "," < BR /> ");
}

Public Static string MD5 (this string S)
{
Md5cryptoserviceprovider provider;
Provider = New md5cryptoserviceprovider ();
Byte [] Bytes = Encoding. utf8.getbytes (s );
Stringbuilder = New stringbuilder ();

Bytes=Provider. computehash (bytes );

Foreach (byte BInBytes)
Builder. append (B. tostring ("X2"). tolower ());

ReturnBuilder. tostring ();
}
}

 

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.