About the inconsistency between PHP MD5 encryption and Java MD5 encryption results

Source: Internet
Author: User
Tags md5 encryption

For PHP is not UTF-8 encoding caused by the problem

public string MD5 (string txt) {
try{
MessageDigest MD = messagedigest.getinstance ("MD5");
Md.update (Txt.getbytes ("GBK")); The main problem here is that the Java string is Unicode encoding, not affected by the encoding of the source file, and the Code of PHP is consistent with the source code files, affected by the source code.
StringBuffer buf=new StringBuffer ();
For (Byte b:md.digest ()) {
Buf.append (String.Format ("%02x", B&0xff));
}
return buf.tostring ();
}catch (Exception e) {
E.printstacktrace ();

return null;
}
}

or use PHP directly to encrypt it for use in Java.

1, set up the environment of PHP (not to introduce), write a by extracting the get parameters, and the value of the MD5 encryption page, as follows

<?php Echo Strtoupper (MD5 ($_get["MD5STR"]);?>

Strtoupper is a function of letter capitalization conversion

MD5 is a function of MD5 encryption

$_get["Md5str"] is the URL with a md5str parameter, the value is obtained and printed out

2, the Java page submission method

/**
* For PHP submission processing
* @param URL
*/
public static string phprequest (string url) {
try{
HttpClient client = new HttpClient ();
Postmethod post = new Postmethod (URL);//use Post to submit data
Post.setrequestheader ("Content-type", "text/html; Charset=utf-8");
Client.executemethod (POST);
String response = new String (post.getresponsebodyasstring (). GetBytes ("8859_1"), "UTF-8");//Print results page
Post.releaseconnection ();
return response;
} catch (IOException e) {
E.printstacktrace ();
return null;
}
}

Need to be prompted, the URL remembers the Chinese parameter first UTF-8 encoding and then to the method inside, this method of the response to the results of the anti-coding processing, and finally can correctly return the value of PHP MD5 encrypted!

About the inconsistency between PHP MD5 encryption and Java MD5 encryption results

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.