Skip to main content

How to Decode Base64url in Java with jjwt Decoders.BASE64URL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Decoders.BASE64URL.decode(text) from jjwt turns base64url text, such as one piece of a JWT, back into bytes. It's strict: plain base64 with / or = makes it throw a DecodingException instead of guessing.

Syntax​

Syntax
byte[] <bytes> = Decoders.BASE64URL.decode(<text>);
String <decoded> = new String(<bytes>, StandardCharsets.UTF_8);
PartWhat it does
Decoders.BASE64URL.decode(<text>)Decodes base64url text, with or without padding, into a byte[].
new String(<bytes>, StandardCharsets.UTF_8)Turns the bytes into text, when they are text.
DecodingExceptionThrown for a character that base64url doesn't use.

The jjwt API documentation for Decoders: BASE64URL is a "Very fast Base64Url decoder guaranteed to work in all >= Java 7 JDK and Android environments."

The Example​

The example decodes SGk_Pg, the base64url form of Hi?>, and prints it as text. Then it tries SGk/Pg==, the plain base64 form of the same text, and prints the error.

Prerequisites​

  • A JDK and Maven. This guide uses OpenJDK 25.0.5.1 and Maven 3.10.2.

Step 1: Create the Project​

Create a folder named demo with this pom.xml:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

jjwt-api holds the decoders, and exec-maven-plugin lets Maven run the program.

Step 2: Write the Code​

Create src/main/java/com/example/Main.java inside demo:

Main.java, which decodes SGk_Pg with Decoders.BASE64URL and prints it as UTF-8 text, then decodes SGk/Pg== inside try and catch and prints the DecodingException's message.

src/main/java/com/example/Main.java
package com.example;

import io.jsonwebtoken.io.DecodingException;
import io.jsonwebtoken.io.Decoders;

import java.nio.charset.StandardCharsets;

public class Main {
public static void main(String[] args) {
byte[] data = Decoders.BASE64URL.decode("SGk_Pg");
System.out.println(new String(data, StandardCharsets.UTF_8));

try {
Decoders.BASE64URL.decode("SGk/Pg==");
} catch (DecodingException e) {
System.out.println(e.getMessage());
}
}
}

Step 3: Run It​

Terminal
cd demo
mvn -q compile exec:java
Output
Hi?>
Illegal base64url character: '/'

A zsh terminal where mvn compile exec prints Hi?> and then Illegal base64url character: '/'.

-q keeps Maven quiet, compile builds the code, and exec:java runs the class named in pom.xml.

What Happened​

  • SGk_Pg decoded to the bytes of Hi?> without any padding.
  • SGk/Pg== stopped at the /, which base64url doesn't use, and the decoder threw DecodingException: Illegal base64url character: '/'.

Common Mistakes​

MistakeWhat happensDo this instead
Decoding plain base64 with BASE64URLDecodingException: Illegal base64url character: '/'Use Decoders.BASE64 for plain base64
Decoding base64url with Decoders.BASE64DecodingException: Illegal base64 character: '_'Use Decoders.BASE64URL
Turning binary data, such as a signature, into a StringThe bytes aren't text and come out garbledKeep them as byte[]

Troubleshooting​

SymptomLikely causeFix
Illegal base64url character: '*' or another characterThe input isn't base64url, or has a stray characterCheck where the text came from
cannot find symbol: class DecodingExceptionThe import is missingAdd import io.jsonwebtoken.io.DecodingException;

FAQ​

Can I decode a JWT's payload with this?​

Yes: split the token at its dots and decode the middle piece. That only reads the claims; it doesn't verify them, so use it for reading, and verify the token with jjwt's parser before trusting anything in it.

Does it need the padding?​

No. SGk_Pg decoded without the = that plain base64 would add.

Conclusion​

Decoders.BASE64URL.decode(text) turns base64url into bytes and refuses anything else, and new String(bytes, StandardCharsets.UTF_8) turns text bytes back into a string.

Additional Resources​