Skip to content
Related Articles

Related Articles

Basic Type Base64 Encoding and Decoding in Java

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 18 Oct, 2021
Improve Article
Save Article

Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.(Base 64 format reference).
The Basic encoding means no line feeds are added to the output and the output is mapped to a set of characters in A-Za-z0-9+/ character set and the decoder rejects any character outside of this set.
Encode simple String into Basic Base 64 format 

String BasicBase64format= Base64.getEncoder().encodeToString(“actualString”.getBytes()); 
 

Explanation: In above code we called Base64.Encoder using getEncoder() and then get the encoded string by passing the byte value of actualString in encodeToString() method as parameter.
Decode Basic Base 64 format to String 

byte[] actualByte= Base64.getDecoder().decode(encodedString); 
String actualString= new String(actualByte); 
 

Explanation: In above code we called Base64.Decoder using getDecoder() and then decoded the string passed in decode() method as parameter then convert return value to string.
Below programs illustrate the Encoding and Decoding in Java:
Program 1:Encode simple String into Basic Base 64 format

Java




// Java program to demonstrate
// Encoding simple String into Basic Base 64 format
 
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
 
        // create a sample String to encode
        String sample = "India Team will win the Cup";
 
        // print actual String
        System.out.println("Sample String:\n"
                           + sample);
 
        // Encode into Base64 format
        String BasicBase64format
            = Base64.getEncoder()
                  .encodeToString(sample.getBytes());
 
        // print encoded String
        System.out.println("Encoded String:\n"
                           + BasicBase64format);
    }
}


Output: 

Sample String:
India Team will win the Cup
Encoded String:
SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw

 

Program 2: Decode Basic Base 64 format to String

Java




// Java program to demonstrate
// Decoding Basic Base 64 format to String
 
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
 
        // create an encoded String to decode
        String encoded
            = "SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw";
 
        // print encoded String
        System.out.println("Encoded String:\n"
                           + encoded);
 
        // decode into String from encoded format
        byte[] actualByte = Base64.getDecoder()
                                .decode(encoded);
 
        String actualString = new String(actualByte);
 
        // print actual String
        System.out.println("actual String:\n"
                           + actualString);
    }
}


Output: 

Encoded String:
SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
actual String:
India Team will win the Cup

 

References: 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!