Base32 Encoding and Decoding in Java using Apache Commons Codec

Tags: String Base32 Apache Commons Apache Commons Codec commons-codec

In this Java tutorial, We learn how to use the Base32 class of Apache Commons Codec library to do Base32 encoding and decoding in Java programs.

How to add Apache Commons Codec library to your Java project

To use the Apache Commons Codec library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'commons-codec:commons-codec:1.15'

To use the Apache Commons Codec library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.15</version>
</dependency>

To have more information about the Apache Commons Codec library you can visit the library home page at commons.apache.org/proper/commons-codec/

How to encode Base32 String

In the following Java program, we use the Base32.encodeAsString() method to encode a byte array into Base32 String.

Base32EncodeExample1.java

import org.apache.commons.codec.binary.Base32;

public class Base32EncodeExample1 {
    public static void main(String... args) {
        String inputString = "Simple Solution";
        byte[] inputStringInBytes = inputString.getBytes();

        Base32 base32 = new Base32();
        String encodedString = base32.encodeAsString(inputStringInBytes);

        System.out.println("Input Data: " + inputString);
        System.out.println("Base32 Encoded Data: " + encodedString);
    }
}
The output is:
Input Data: Simple Solution
Base32 Encoded Data: KNUW24DMMUQFG33MOV2GS33O

In the following Java program, we use Base32.encode() which also encode a byte array and return byte array as result.

Base32EncodeExample2.java

import org.apache.commons.codec.binary.Base32;

public class Base32EncodeExample2 {
    public static void main(String... args) {
        String inputString = "Simple Solution";
        byte[] inputStringInBytes = inputString.getBytes();

        Base32 base32 = new Base32();
        byte[] encodedData = base32.encode(inputStringInBytes);
        String encodedString = new String(encodedData);

        System.out.println("Input Data: " + inputString);
        System.out.println("Base32 Encoded Data: " + encodedString);
    }
}
The output is:
Input Data: Simple Solution
Base32 Encoded Data: KNUW24DMMUQFG33MOV2GS33O

How to decode Base32 String

In the following Java example, we show you how to decode a Base32 String using Base32.decode() method.

Base32DecodeExample.java

import org.apache.commons.codec.binary.Base32;

public class Base32DecodeExample {
    public static void main(String... args) {
        String encodedString = "KNUW24DMMUQFG33MOV2GS33O";

        Base32 base32 = new Base32();
        byte[] decodedBytes = base32.decode(encodedString);
        String decodedString = new String(decodedBytes);

        System.out.println("Base32 Encoded Input Data: " + encodedString);
        System.out.println("Base32 Decoded Data: " + decodedString);
    }
}
The output is:
Base32 Encoded Input Data: KNUW24DMMUQFG33MOV2GS33O
Base32 Decoded Data: Simple Solution

Happy Coding 😊

Java Apache Commons Codec Tutorial