yonatan hornstein
yonatan hornstein

Reputation: 233

Base64 Decoding, output is not correct using UTF-8, UTF-16

i've built local restAPI server on my computer, and i have register function, sending user data to mySQL database.

I've encoded the Password String to base64 String (Wanted to try to encrypt my self). When i try to Decode the Password i fetch from the DB, i get wrong output.

Password for example (base64): MDQ1MTA0NTE=

Which will output- 04510451

The decoder class -

byte[] decodedValue = Base64.getDecoder().decode(password);  


return new String(decodedValue, StandardCharsets.UTF_8);

The encoder class -

byte[] passEncoded = Base64.getEncoder().encode(password.getBytes("UTF-8"));
System.out.println("encoded value is " + new String(passEncoded));
String finalPass = null;
finalPass = new String(passEncoded, "UTF-8");

return finalPass;

Now the actual output i get : "ӎuӎu" (needs to be 04510451)

Thanks in advance, Jonathan

Upvotes: 0

Views: 1281

Answers (1)

Marcono1234
Marcono1234

Reputation: 6914

It looks like you are decoding twice. This is not visible in the code snippets you provided but would be the most logical explanation:

MDQ1MTA0NTE= -> 04510451 -> ӎuӎu

Upvotes: 1

Related Questions