codec

Decode Base64

This is an example of how to decode Strings with the Base64 algorithm. We are using the org.apache.commons.codec.binary.Base64 class that provides Base64 encoding and decoding as defined by RFC 2045. Decoding with org.apache.commons.codec.binary.Base64 class implies that you should:

  • Create a String.
  • Get the bytes from the String, using getBytes() API method of String.
  • Use decodeBase64(byte[] base64Data) API method, using the byte array from the String to decode Base64 data into octets.
  • Print the decoded array, using the toString(byte[] a) API method of Arrays.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;
 
import org.apache.commons.codec.binary.Base64;
 
import java.util.Arrays;
 
public class decodeBase64 {
	
    public static void main(String[] args) {
    
    	String string = "SmF2YWNvZGVnZWVrcw==";
 

  // Get bytes from string

  byte[] byteArray = Base64.decodeBase64(string.getBytes());
 

  // Print the decoded array

  System.out.println(Arrays.toString(byteArray));
 

  // Print the decoded string 

  String decodedString = new String(byteArray);

  System.out.println(string + " = " + decodedString);
    }
}

Output:

[74, 97, 118, 97, 99, 111, 100, 101, 103, 101, 101, 107, 115]
SmF2YWNvZGVnZWVrcw== = Javacodegeeks

  
This was an example of how to decode Strings with the Base64 algorithm in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button