Base64_en code Java和PHP之间的不同 [英] Base64_encode different between Java and PHP

查看:1639
本文介绍了Base64_en code Java和PHP之间的不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:

我有一个JAVA功能来生成一个加密的字符串。我必须做在PHP中同样的事情。

I have a JAVA function to generate an encrypted string. I have to do the same thing in PHP.

我的Java功能:

String generateSignature () {
    byte[] Sequence = ("hello").getBytes("UTF-8");
    Mac HMAC = Mac.getInstance("HMACSHA256");
    HMAC.init("SECRET_KEY");
    byte[] Hash = HMAC.doFinal(Sequence);
    String Signature = new String(Base64.encodeBase64(Hash));
    return Signature;
}

我的PHP函数:

My PHP function :

function generateSignature() {
    $sequence = "hello";
    $encrypted = hash_hmac('sha256', $sequence, "SECRET_KEY");
    return base64_encode($encrypted);
}

的两个函数的返回值是不相同的。我注意到的是,在编码至基部64之前,这两个函数具有相同的结果。所以,对我来说,问题不是对密钥的生成,但是,从编码。

The return value of the two functions are not the same. What I noticed is that before the encoding to base 64, both functions have the same result. So, for me the problem is not on the generation of the key but on the encoding.

任何人能帮助吗?

推荐答案

答案是在的文档为PHP函数hash_hmac。

The answer is in the documentation for the PHP function hash_hmac.

当设置为TRUE,输出原始二进制数据。 FALSE输出小写hexits。

When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.

传递真作为最后一个参数。哈希是二进制。当把它们变成字符串,他们往往都设有十六进制codeD。但在这种情况下,你要立足-64连接code,所以你想要的原始二进制形式。

Pass "true" as the final argument. Hashes are binary. When turning them into strings, they are often encoded in hexadecimal. But in this case you are going to base-64 encode it, so you want the raw binary form.

这篇关于Base64_en code Java和PHP之间的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆