Jake Burton
Jake Burton

Reputation: 896

Why does android.util.Base64 return a different string?

I'm using android.util.Base64 to encode a username + password for HTTP Basic Authentication like so:

String encoded = Base64.encodeToString((username+":"+password).getBytes(), Base64.NO_WRAP);

If I used "user" & "pass" for the username and password I would expect to get dXNlcjpwYXNzCg== (from openssl)

echo 'user:pass' | openssl base64

but instead I get this:

returned string

Any ideas why this is?

Thanks,

Jake

Note: Simply passing the resulting string to a show progress dialog:

Tools.ShowProgress(encoded, Login.this);

Upvotes: 2

Views: 2832

Answers (1)

Brendan Long
Brendan Long

Reputation: 54232

(username+":"+password).getBytes()

String.getBytes() returns the bytes of the string in the current platform encoding. To ensure you match other platforms, you probably want to use String.getBytes(Charset) with UTF-8:

(username+":"+password).getBytes("UTF-8")

If that's not the issue, another thing to check is if your input is what you think it is:

String input = username+":"+password;
log.warn("Input was: " + input);
// calculate base64

Upvotes: 5

Related Questions