MD5 consistency in Java and. net

Source: Internet
Author: User

Recently, the cool interface was jointly tuned with Dongxin beiyou. In this interface, MD5 computing is required to verify the identity of each other. The other party uses Java for development.. NET development. Unfortunately, the MD5 Algorithm of the other party does not match the test siute of RFC 1321. They provided the MD5 algorithm. After multiple times of searching for information and asking people who want to ask for information, they finally achieved consistency. The code is provided for reference by people who have similar problems with me:

1. Java Edition

Code

 /**
* Generate a 32-bit MD5 message digest
*
* @ Param info
* Original string message
* @ Return
*/
Public static string getdigeststr (string info ){
Try {
Byte [] res = info. getbytes ();
Messagedigest MD = messagedigest. getinstance ("MD5 ");
Byte [] result = md. Digest (RES );
For (INT I = 0; I <result. length; I ++ ){
Md. Update (result [I]);
}
Byte [] hash = md. Digest ();
Stringbuffer d = new stringbuffer ("");
For (INT I = 0; I Int v = hash [I] & 0xff;
If (v <16 ){
D. append ("0 ");
}
D. append (integer. tostring (v, 16). touppercase ());
}
Return D. tostring ();
} Catch (exception e ){
Return NULL;
}
}

 

2.. Net Edition

Code

 /// <Summary>
/// Corresponding to the Java version of MD5 (two consecutive summaries)
/// </Summary>
/// <Param name = "info"> </param>
/// <Returns> </returns>
Public static string getdigeststr (string info)
{
Try
{
Byte [] res = system. Text. encoding. Default. getbytes (Info );
Md5cryptoserviceprovider MD = new md5cryptoserviceprovider ();

Byte [] result = md. computehash (RES );
Byte [] hash = md. computehash (result );
Stringbuilder sbuilder = new stringbuilder ();

For (INT I = 0; I {
Int v = hash [I] & 0xff;
If (v <16) sbuilder. append ("0 ");

Sbuilder. append (convert. tostring (v, 16). toupper ());
}

Return sbuilder. tostring ();
}
Catch
{
Return NULL;
}
}

 

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.