base64 command on macOS returns wrong result

7.5k views Asked by At

When calling base64 <<< username:password I get this result: dXNlcm5hbWU6cGFzc3dvcmQK.

When using that result in PostMan and issuing a request against a Basic Auth endpoint, I get back a 401.

When encoding username:password at https://www.base64encode.org, I get back this result: dXNlcm5hbWU6cGFzc3dvcmQ= and I can successfully use it against the Basic Auth endpoint from above.

If I use PostMan to generate the Basic Auth header passing in username and password, it generates the same base64 encoded string as https://www.base64encode.org. I could also copy username:password into testin.txt and use openssl to create the base64 string:

openssl base64 -in testin.txt -out testout.txt returns dXNlcm5hbWU6cGFzc3dvcmQK which is the same wrong result as base64 creates.

openssl version returns OpenSSL 0.9.8zh 14 Jan 2016

3

There are 3 answers

4
Joel Rondeau On BEST ANSWER

Decoding dXNlcm5hbWU6cGFzc3dvcmQK gives username:password\n.

So my guess is that in both cases, you have a linefeed you weren't expecting. The testin.txt case is easy to solve (modify file, remove linefeed).

I expect the base64 case can be solved by using echo with the -n parameter to remove the linefeed:

echo -n username:password | base64 --encode
0
Amrut Prabhu On

On mac OS 10.15.6

The --encode option is not available. So this works without including the the --encode option.

echo -n username:password | base64 
0
Stefan Kühn On

BASE64 file conversion on macOS (currently using OS X 10.14.6):

Encoding (PDF -> BASE64): cat aaa.pdf | base64 > aaa.base64

Decoding (BASE64 -> PDF): cat aaa.base64 | base64 -D > aaa.pdf

I' using a PDF-File here as a sample. Of course you can use any file type.