js-base64
js-base64 copied to clipboard
Cannot decode js-base64 UTF-8 encoded String
Hi! I'm trying to decode a string containing an UTF-8 character from the javascript library to Java server:
My test: index.js
import { Base64 } from 'js-base64';
function App() {
console.log(Base64.encode("louis:€"));
}
Decoder.java
class DecoderTest{
@org.junit.jupiter.api.Test
void readToken() {
String x = new String(Base64.getEncoder().encode("louis:€".getBytes(StandardCharsets.UTF_8)));
Assertions.assertEquals("louis:€", readToken2(x));
Assertions.assertEquals("louis:€", readToken2("Basic bG91aXM64oKs"));
}
public static String readToken2(String token) {
String cleanToken = token.replace("Basic ", "");
byte[] bytesToken = cleanToken.getBytes(StandardCharsets.UTF_8);
byte[] bytesDecoded = Base64.getDecoder().decode(bytesToken);
String decoded = new String(bytesDecoded, StandardCharsets.UTF_8);
return decoded;
}
}
The output of the test:
org.opentest4j.AssertionFailedError:
Expected :louis:€
Actual :louis:�
<Click to see difference>