Base64_encode diferente entre Java y PHP

Este es mi problema:

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

My Java function :

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

My PHP function :

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

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.

Anybody able to help please ?

preguntado el 12 de junio de 12 a las 19:06

What was the difference? -

Hello.. I think you was working with woocommerce apis. I also have the same problem. Did you succeded in connecting to apis with java.? Please help me i m getting signature dont match error -

1 Respuestas

La respuesta está en el documentación for the PHP function hash_hmac.

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

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.

Respondido el 13 de junio de 12 a las 01:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.