/// <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 ();
}
}