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.


NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

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