Development

How to Decode and Encode Base64 in Java

A comprehensive guide to using java.util.Base64 for standard, URL-safe, and MIME Base64 encoding in modern Java applications.

Published on August 18, 20266 min read

Prior to Java 8, developers had to rely on third-party libraries like Apache Commons Codec or undocumented internal Sun APIs (sun.misc.BASE64Encoder) for Base64 operations.

Fortunately, starting with Java 8, the standard library includes a dedicated, highly optimized java.util.Base64 class. It natively supports Basic, URL-safe, and MIME Base64 variants.

Warning on Legacy Code

If you are updating an older Java application, you might find imports for sun.misc.BASE64Encoder. This internal API was officially removed in Java 9. You should always migrate to java.util.Base64.

1. Standard Base64 Encoding in Java

To encode a string using the standard Base64 alphabet, you retrieve the Base64.Encoder using Base64.getEncoder(). Note that you must convert your String to bytes using a specific charset (usually UTF-8).

java
import java.util.Base64;
import java.nio.charset.StandardCharsets;

public class Base64Example {
    public static void main(String[] args) {
        String originalText = "Hello, Java Base64!";
        
        // 1. Convert string to UTF-8 bytes
        byte[] textBytes = originalText.getBytes(StandardCharsets.UTF_8);
        
        // 2. Encode bytes to Base64 string
        String encodedText = Base64.getEncoder().encodeToString(textBytes);
        
        System.out.println("Encoded: " + encodedText);
        // Output: Encoded: SGVsbG8sIEphdmEgQmFzZTY0IQ==
    }
}

2. Standard Base64 Decoding in Java

To decode a Base64 string, you use the Base64.Decoder via Base64.getDecoder(). It will throw an IllegalArgumentException if the string is not valid Base64.

java
import java.util.Base64;
import java.nio.charset.StandardCharsets;

public class Base64Example {
    public static void main(String[] args) {
        String encodedText = "SGVsbG8sIEphdmEgQmFzZTY0IQ==";
        
        try {
            // 1. Decode Base64 string into bytes
            byte[] decodedBytes = Base64.getDecoder().decode(encodedText);
            
            // 2. Convert bytes back to a UTF-8 string
            String decodedText = new String(decodedBytes, StandardCharsets.UTF_8);
            
            System.out.println("Decoded: " + decodedText);
            // Output: Decoded: Hello, Java Base64!
        } catch (IllegalArgumentException e) {
            System.out.println("Invalid Base64 string");
        }
    }
}

3. URL-Safe Base64 in Java

Java natively supports the URL and Filename safe Base64 alphabet (RFC 4648). It uses - and _ instead of + and /.

java
import java.util.Base64;
import java.nio.charset.StandardCharsets;

public class Base64UrlExample {
    public static void main(String[] args) {
        String urlData = "subjects?dir=/usr/local";
        byte[] bytes = urlData.getBytes(StandardCharsets.UTF_8);
        
        // Use getUrlEncoder() instead of getEncoder()
        String safeToken = Base64.getUrlEncoder().encodeToString(bytes);
        
        System.out.println("URL Safe: " + safeToken);
        // Output: c3ViamVjdHM_ZGlyPS91c3IvbG9jYWw=
        
        // If you need to omit the padding '=' completely:
        String noPadding = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
        System.out.println("No Padding: " + noPadding);
    }
}

Need a quick conversion?

Skip the code and use our interactive web tool to instantly encode or decode Base64 strings, including full support for URL-safe formats.

Go to the Base64 Converter Tool