Site icon

Base64 Encode & Decode of password string or byte array (Java8)

Problem Statement ?

Example of Base64 Encoding and decoding of String:

JDK classes for Base 64 encoding & decoding:

Code: Encode & decode byte/ string using Base64 (Java8)

package org.learn;

import java.util.Base64;

public class Base64EncodingDemo {

    public static void main(String[] args) {
        String input = "I Love Soccer";
        byte[] bytesInput = input.getBytes();
        
        System.out.println("Input String : "+input);
        
        //Encode String to Base64
        Base64.Encoder base64Encoder = Base64.getEncoder();
        String encodedString = base64Encoder.encodeToString(bytesInput);
        System.out.println("Encoded String :" + encodedString);

        //Decode Base64 encoded string to String
        Base64.Decoder base64Decoder = Base64.getDecoder();
        byte[] byteDecoded = base64Decoder.decode(encodedString);
        System.out.println("Decoded String :" + new String(byteDecoded));
    }
}

Output: Base64 encoding & decoding of byte array (Java8)

Input String : I Love Soccer
Encoded String :SSBMb3ZlIFNvY2Nlcg==
Decoded String :I Love Soccer
Exit mobile version