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.