java的代码:
public static String encoderByMd5(String str){
MessageDigest md5;
String newstr = "";
try{
md5 = MessageDigest.getInstance("MD5");
BASE64Encoder encoder = new BASE64Encoder();
try{
newstr=encoder.encode(md5.digest(str.getBytes("utf-8")));
newstr = newstr.replaceAll("=", "");
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
return newstr;
}
php的代码:
function javaMd5($str){
$str = md5($str);
$str = base64_encode($str);
$str = str_replace("=","",$str);
return $str;
}
如何使PHP加密数据与Java加密数据相同?请帮帮我!
答案 0 :(得分:0)
我认为这些是两种方法md5.digest(str.getBytes(" utf-8"))和md5($ str)......
答案 1 :(得分:0)
试试这个
public static String encoderByMd5(String str){
MessageDigest md5;
String newstr = "";
try{
md5 = MessageDigest.getInstance("MD5");
BASE64Encoder encoder = new BASE64Encoder();
try{
StringBuilder builder = new StringBuilder();
for (byte b: md5.digest(str.getBytes("utf-8"))) {
builder.append(String.format("%02X", b & 0xff));
}
newstr = encoder.encode(builder.toString().getBytes());
newstr = newstr.replaceAll("=", "");
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}catch(NoSuchAlgorithmException e) {
e.printStackTrace();
}
return newstr.toUpperCase();
}