Illegal base64 character 5f, Illegal base64 character 2d and java.util.Base64 📎
Decoding a String with invalid Base64 '_'
character:
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.junit.jupiter.api.Test;
public class Base64EncodeTest {
@Test
public void decodeUnderscode() {
byte[] bytes = "CAFEHELLO_BABEDUKE".getBytes(StandardCharsets.UTF_8);
Base64.getDecoder().decode(bytes);
}
}
raises the java.lang.IllegalArgumentException: Illegal base64 character 5f
'-'
:
@Test
public void decodeMinus() {
byte[] bytes = "CAFEHELLO-BABEDUKE".getBytes(StandardCharsets.UTF_8);
Base64.getDecoder().decode(bytes);
}
...leads to: java.lang.IllegalArgumentException: Illegal base64 character 2d
Both characters can be decoded with Base64.getUrlDecoder
:
@Test
public void urlDecode() {
byte[] underscored = "CAFEHELLO_BABEDUKE".getBytes(StandardCharsets.UTF_8);
Base64.getUrlDecoder().decode(underscored);
byte[] dashed = "CAFEHELLO-BABEDUKE".getBytes(StandardCharsets.UTF_8);
Base64.getUrlDecoder().decode(dashed);
}
See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.