Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EncodeBase64 in JAVA return a wrong output

I am building a web using Spring. I want to encode string to send as a Basic Authentication.

Here is the code,

private String authUsername = "admin";
private String authPassword = "admin"; 
private String unEncode = authUsername+authPassword;
private byte[] encodedBytes = Base64.encodeBase64(unEncode.getBytes());
private String encode = new String(encodedBytes);   
private String authenCode = "Basic "+encode;

The result is "Basic YWRtaW5hZG1pbg==" And it is wrong.

The right output should be "Basic YWRtaW46YWRtaW4="

What I did wrong or What I missed ?

Thanks.

like image 689
Hikaru Shindo Avatar asked Nov 25 '25 13:11

Hikaru Shindo


1 Answers

The problem is not in the Base64-encoding but in constructing your string. Basic-Authentication requires username:password but your are missing the :.

like image 87
piet.t Avatar answered Nov 28 '25 01:11

piet.t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!