2

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);
1
  • Remember that echo prints a \n character at the end of the line. avoid this with the -n option: echo -n "user:pass" | openssl base64 Commented Sep 27, 2012 at 19:14

1 Answer 1

5
(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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Turns out I missed out a getText() on one of my inputs instead of username.getText().toString() I had username.toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.