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

and the character '-':

@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.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License