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