base64.decode in Java

Mohammad Irfan Oct 12, 2023
  1. Encoding and Decoding Methods of Base64 Class
  2. Base64 Encoding in Java
  3. Base64 Decoding in Java
  4. Encoding and Decoding using Apache Commons Codec Library in Java
  5. Encoding and Decoding URL in Java
  6. Mime Encoding and Decoding in Java
base64.decode in Java

This tutorial introduces what is base64.decode in Java, and how to use it to decode the encoded data in Java.

Java added a new class, Base64, in its Java 8 version. This class has several nested classes such as encoder and decoder that can be used to encode and decode data. It allows several encoded features such as basic encoding, URL encoding, MIME encoding, and decoding support for each as well. You can see below the methods for each purpose.

Encoding and Decoding Methods of Base64 Class

// Basic Encoding Decoding
public static Base64.Decoder getDecoder() public static Base64
    .Encoder getEncoder()

    // Url Encoding Decoding
    public static Base64.Decoder getUrlDecoder() public static Base64
    .Encoder getUrlEncoder()

    // Mime Encoding Decoding
    public static Base64.Decoder getMimeDecoder() public static Base64.Encoder getMimeEncoder()

In this article, we will learn to encode and decode basic and other types of encoding. Let’s start with some examples.

Base64 Encoding in Java

This is a basic encoding example. Java provides getEncoder() method to encode the data. See the example below.

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;
public class SimpleTesting {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String msg = "This is confidential";
    System.out.println("Message  before encoding: " + msg);
    byte[] bytes = msg.getBytes("UTF-8");
    Encoder encoder = Base64.getEncoder();
    String encoded = encoder.encodeToString(bytes);
    System.out.println("Message after encoding: " + encoded);
  }
}

Output:

Message  before encoding: This is confidential
Message after encoding: VGhpcyBpcyBjb25maWRlbnRpYWw=

Base64 Decoding in Java

Here, we used the getDecoder() method to decode the encoded data. This method returns the result in bytes. So, to verify, we used a String constructor to get the data into String back.

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;
public class SimpleTesting {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String msg = "This is confidential";
    System.out.println("Message  before encoding: " + msg);
    byte[] bytes = msg.getBytes("UTF-8");
    Encoder encoder = Base64.getEncoder();
    String encoded = encoder.encodeToString(bytes);
    System.out.println("Message after encoding: " + encoded);
    byte[] decoded = Base64.getDecoder().decode(encoded);
    String decodedMsg = new String(decoded);
    System.out.println("Decoded Message " + decodedMsg);
  }
}

Output:

Message before encoding: This is confidential
Message after encoding: VGhpcyBpcyBjb25maWRlbnRpYWw=
Decoded Message This is confidential

Encoding and Decoding using Apache Commons Codec Library in Java

This is another solution to encode and decode data in Java. Here, we are using the codec library and its Base64 class. It provides the encodeBase64() method that returns an encoded string that can be decoded further by using the decodeBase64() method. See the example below.

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;

public class SimpleTesting {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String msg = "This is confidential";
    System.out.println("Message  before encoding: " + msg);
    byte[] bytes = msg.getBytes("UTF-8");
    String encoding = new String(Base64.encodeBase64(bytes));
    System.out.println("---------After Encoding-----------");
    System.out.println(encoding);

    // Decoding
    String decoding = new String(Base64.decodeBase64(encoding));
    System.out.println("---------After Decoding----------");
    System.out.println(decoding);
  }
}

Output:

Message  before encoding: This is confidential
---------After Encoding-----------
VGhpcyBpcyBjb25maWRlbnRpYWw=
---------After Decoding----------
This is confidential

Encoding and Decoding URL in Java

As we stated at starting, Java Base64 can be used to encode and decode URLs as well. So, in this example, we used getUrlEncoder() method for encoding and getUrlDecoder() method to decode the URL. See the example below.

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;
public class SimpleTesting {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String url = "https://www.delftstack.com/";
    System.out.println("Message  before encoding: " + url);
    byte[] bytes = url.getBytes("UTF-8");
    Encoder encoder = Base64.getUrlEncoder();
    String encoded = encoder.encodeToString(bytes);
    System.out.println("Message after encoding: " + encoded);
    // Decoding
    String decoding = new String(Base64.getUrlDecoder().decode(encoded));
    System.out.println("---------After Decoding----------");
    System.out.println(decoding);
  }
}

Output:

Message  before encoding: https://www.delftstack.com/
Message after encoding: aHR0cHM6Ly93d3cuZGVsZnRzdGFjay5jb20v
---------After Decoding----------
https://www.delftstack.com/

Mime Encoding and Decoding in Java

To encode mime data, Java provides getMimeEncoder() method that returns an encoded bytes and to decode that Java provides getMimeDecoder() method. See the example below.

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;

public class SimpleTesting {
  public static void main(String[] args) throws UnsupportedEncodingException {
    String msg = "This is \n confidential \n file";
    System.out.println("Message  before encoding: " + msg);
    byte[] bytes = msg.getBytes("UTF-8");
    Encoder encoder = Base64.getMimeEncoder();
    String encoded = encoder.encodeToString(bytes);
    System.out.println("Message after encoding: " + encoded);
    // Decoding
    String decoding = new String(Base64.getMimeDecoder().decode(encoded));
    System.out.println("---------After Decoding----------");
    System.out.println(decoding);
  }
}

Output:

Message  before encoding: This is 
 confidential 
 file
Message after encoding: VGhpcyBpcyAKIGNvbmZpZGVudGlhbCAKIGZpbGU=
---------After Decoding----------
This is 
 confidential 
 file