java's code:
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's code:
function javaMd5($str){
$str = md5($str);
$str = base64_encode($str);
$str = str_replace("=","",$str);
return $str;
}
How can I make PHP encrypted data the same as Java encrypted data? Please help me!
我认为这些是md5.digest(str.getBytes(“ utf-8”))和md5($ str)的2种方法...
Try this
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.