Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP';MD5和Java';s MD5数据不匹配_Java_Php_Md5 - Fatal编程技术网

PHP';MD5和Java';s MD5数据不匹配

PHP';MD5和Java';s MD5数据不匹配,java,php,md5,Java,Php,Md5,java的代码: public static String encoderByMd5(String str){ MessageDigest md5; String newstr = ""; try{ md5 = MessageDigest.getInstance("MD5"); BASE64Encoder encoder = new BASE64Encoder(); try{ newstr=e

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加密数据相同?请帮帮我

我认为这是两种方法md5.digest(str.getBytes(“utf-8”))和md5($str).

我认为这是两种方法md5.digest(str.getBytes(“utf-8”))和md5($str).

试试这个

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();
}
试试这个

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();
}

这应该有帮助:)你能提供样本输出吗?我已经搜索了BASE64Encoder,但找不到任何东西。另外,如果从base64编码字符串中删除
=
,以后可能无法对其进行解码。我不需要解码,只用于加密使用
md5($str,true)
要获得原始MD5,因为Java也将获得原始MD5,这应该会有所帮助:)您可以提供示例输出吗?我搜索了BASE64Encoder,但找不到任何内容。另外,如果从base64编码字符串中删除
=
,以后可能无法对其进行解码。我不需要解码,只用于加密使用
md5($str,true)
获得原始md5,因为Java也将获得原始md5