0

I have to decode the authorization header of an http request of the form Basic bW9uTG9naW46bW9uTW90RGVQYXNz

When I decode it online, I got the correct result monLogin:monMotDePass.

When I try with my code :

String valueDecoded = Base64.getDecoder().decode(request.getHeader("Authorization").split(" ")[1]).toString();
System.out.println(valueDecoded.toString());

I've got this result which is not correct : [B@16d9ea61

Is there anything wrong with my line ?

Community
  • 1
  • 1
user54517
  • 2,020
  • 5
  • 30
  • 47
  • 1
    Possible duplicate of [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Progman Sep 28 '19 at 21:04
  • I don't get it :/ – user54517 Sep 28 '19 at 21:05
  • 1
    That `[B@16d9ea61` seems to be an object reference of a byte array, while you probably want its contents. – jbx Sep 28 '19 at 21:09

1 Answers1

3

Don't call toString() on a byte[]. You can construct a String by passing the byte[] to new String. Like,

String valueDecoded = new String(Base64.getDecoder().decode("bW9uTG9naW46bW9uTW90RGVQYXNz"));

I get monLogin:monMotDePass

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249