2
我在堆棧溢出中看到了一些相同的問題,但它對我沒有幫助。php base64_encode hash_hmac和java給出了不同的結果
我有這樣的PHP代碼
$signature=base64_encode(hash_hmac("sha256", trim($xmlReq), $signature_key, True));
我想寫的java等同於,這是我的Java代碼。
public static String encodeXML(String key, String data) {
String result = "";
try {
Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
mac.init(secretKeySpec);
result = Base64.encodeBase64String(mac.doFinal(data.getBytes("UTF-8")));
} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) {
log.error("exception occured when encording HmacSHA256 hash");
}
return result;
}
但他們給出了不同的結果。 有人幫忙。
Base64.Encoder是在Java 8中。如何使用Java 7做到這一點? –
使用[Apache Commons Codec](http://commons.apache.org/proper/commons-codec/) – jasonlam604
謝謝,它工作。 :) –