2

I want to simulate this JAVA code:

String auth = "ec65450a-5:5217e"; byte[] encodedAuth = Base64.encodeBase64(auth.getBytes()); String authHeader = "Basic " + new String(encodedAuth); 

to the PHP like this:

$string = 'ec65450a-5:5217e'; $bytes = array(); $bytes = unpack('C*', $string); $authHeader = 'Basic ' . base64_encode(implode('', $bytes)); 

But PHP code generate another value.

7
  • Wgat value does it generate? Commented Jan 9, 2019 at 18:47
  • @Dharman I can't see JAVA output but this came from PHP: Basic NjU2MzM2MzUzNDM1MzA2MTJkMzUzYTM1MzIzMTM3NjU= Commented Jan 9, 2019 at 18:53
  • Could you please create Ideone.com snippet and another for PHP and paste in the link? Commented Jan 9, 2019 at 18:58
  • 1
    Most likely the encoding is different, e.g. UTF-8, UTF-16, therefore the byte arrays are different and thus the base64 string. Commented Jan 9, 2019 at 19:00
  • 1
    implode('', $bytes) I don't think this is what you want. This gives you a string of integer byte values, like 10099 for string ef. This can't be reversed on the server side because "10,199" and "101,99" encode to the same thing. I think you just want base64_encode($string). Commented Jan 9, 2019 at 19:05

2 Answers 2

2

PHP will already treat $string as a byte string, so you don't need to unpack/implode it.

If you do this:

$string = 'ec65450a-5:5217e'; $bytes = unpack('C*', $string); echo implode('', $bytes); 

You get this:

1019954535253489745535853504955101 

Which is a mushed together list of integer base 10 ASCII values of each character, and is almost certainly not what you want. Just encode the string directly:

echo base64_encode($string); 

Result:

ZWM2NTQ1MGEtNTo1MjE3ZQ== 

Also, you'll want to change your password now that you've posted it here. :)

Sign up to request clarification or add additional context in comments.

Comments

1

Check the character set that the encoders (Java and PHP) are using.

JavaDoc for the : getBytes() method of the String class.

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the default charset is unspecified.

The java.nio.charset.CharsetEncoder class should be used when more control over the encoding process is required.

Returns:The resultant byte array

Since:JDK1.1

If you want to use a specific character set, you can pass it over to the method:

"test".getBytes(StandardCharsets.UTF_8); 

Example This code produces two different values:

String s = "ec65450a-5:5217e"; System.out.println(Base64.getEncoder().encodeToString(s.getBytes())); System.out.println(Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_8))); System.out.println(Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.UTF_16))); 

Output

ZWM2NTQ1MGEtNTo1MjE3ZQ==

ZWM2NTQ1MGEtNTo1MjE3ZQ==

/v8AZQBjADYANQA0ADUAMABhAC0ANQA6ADUAMgAxADcAZQ==

The first two are the same, because getBytes() uses the platform's default charset, and it happens to be UTF-8

4 Comments

But JAVA code for the our old system and work properly. We should not edit it just we want to convert it to PHP. I guess first one without charset is preferred in this situation.
But then it can produce different results when deployed on different platforms with different default charset. That's risky.
Both API only systems hosted by Google cloud. But we want to give APIs to other users in the future. So as you said, they will have problem with use our APIs and our documentations?
It doesn't mean they will have problems, but it's risky to rely on platform's default charset, because if it changes, the result of getBytes() may change (it doesn't mean that it MUST change though).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.