php 與 java 漢語言 md5 不一樣
來源:互聯網
上載者:User
php 與 java 中文 md5 不一樣?
項目組要做個組件與別的公司進行通訊,他們那邊是php 開發,我們這邊的是java,URL 地址是md5加密,我每次調取時,都發生錯誤..這個問題我困擾二三天,最後發現java 中中文的md5與php 中的中文 md5 不一樣,英文完全沒有問題...
解決方案一:php 與 java 兩邊自己 寫md5演算法..
解決方案二:通過java 調用php 頁面,url 參數產生md5 格式,再與別的公司進行通訊
代碼如下:
java 端 :
public static String EncoderByMd5(String str,String url){
str=java.net.URLEncoder.encode(str,"gb2312");CommHttpInterface http = new CommHttpInterface();
return http.phpRequest(url+"?"+str);
}
public static String phpRequest(String url){
try{
HttpClient client = new HttpClient();
PostMethod post = new PostMethod(url);//使用POST方式提交資料
post.setRequestHeader("Content-Type","text/html;charset=gb2312");
client.executeMethod(post);
String response = new String(post.getResponseBodyAsString().getBytes("gb2312"));//列印結果頁面
post.releaseConnection();
return response;
} catch(IOException e){
e.printStackTrace();
return null;
}
}
php 端:
header("Content-type: text/html; charset=gb2312");
$paramStr= urldecode($_GET["md5str"]);
echo strtolower(md5($paramStr));
?>