Logo

dev-resources.site

for different kinds of informations.

Base 64 Encoder-Decoder in Java

Published at
8/26/2021
Categories
java
encoding
decoding
base64
Author
ankurlakhmara
Categories
4 categories in total
java
open
encoding
open
decoding
open
base64
open
Author
13 person written this
ankurlakhmara
open
Base 64 Encoder-Decoder in Java

Encoding is a way to encode anything as it is, without any line separation. Where output generated is the character set A-Za-z0–9+/, and the decoder rejects any characters outside of that set.

Encoding

Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String encodeInput = Base64.getEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8));
        System.out.println("Encoded output is :"+encodeInput);
Enter fullscreen mode Exit fullscreen mode

For Example if we give input "hey ankur"
Then the output will be : aGV5IGFua3Vy

Decoding

        byte[] output = Base64.getDecoder().decode(encodeInput);
        String decodeOutput = new String(output);
        System.out.println("The decoded output is :"+decodeOutput);
Enter fullscreen mode Exit fullscreen mode

If we gives the same output that is encoded by the base-64 encoder as we write above then the output we will get :
is "hey ankur"

How Base 64 Encoding Works?

Base64 encoding is used when any binary data needs to be transmitted over a media that is designed to handle only textual data. Many communication protocols like SMTP, NNTP were traditionally designed to work with plain text data represented by the 7-bit US-ASCII character set. To transfer non-ASCII or binary data over such communication channels, the binary data is encoded to the ASCII charset using Base64 encoding scheme.

The encoding process converts binary data to a printable ASCII string format. The decoding process converts the encoded string back to binary data. for detailed information click here

Complete java program for Encoding and Decoding using Base 64

package Basic_project;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Scanner;

public class encoder_decoder {
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        String encodeInput = Base64.getEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8));
        System.out.println("Encoded output is :"+encodeInput);

        byte[] output = Base64.getDecoder().decode(encodeInput);
        String decodeOutput = new String(output);
        System.out.println("The decoded output is :"+decodeOutput);
    }
}
Enter fullscreen mode Exit fullscreen mode
encoding Article's
30 articles in total
Favicon
Why I Built the Laravel Encoding Package I Couldn’t Find Anywhere Else
Favicon
From 'A' to '😊': How Programming Languages Handle Strings
Favicon
Base64 strings concepts in different programming language
Favicon
Secure and Scalable Encoding Made Easy with Laravel Encoder: A Complete Tutorial
Favicon
Encoding
Favicon
On Transformers and Vectors
Favicon
The ü/ü Conundrum
Favicon
Unlocking the Potential of Video Transcoding
Favicon
How to inverse transform both ordinal and label encoding?
Favicon
Introducción a Buffer en JavaScript
Favicon
Intl.Segmenter(): Don't use string.split() nor string.length
Favicon
Packing and unpacking bytes
Favicon
Chuw Vidf Nam sogp sogp 4.0 (Cvnss4.0) zujx goc nhinl mas hoaj
Favicon
The Hitchhiker's Guide to Binary-to-Text Encoding
Favicon
Text versus bytes
Favicon
Transforming Categorical Data: A Practical Guide to Handling Non-Numerical Variables for Machine Learning Algorithms.
Favicon
Dealing with Categorical Data: Encoding Features for ML Algorithms
Favicon
Application of Media Processing Technology to 4K/8K FHD Video Processing
Favicon
Base64's goodness
Favicon
How does Base64 work?
Favicon
Ordinal Vs One Hot Vs Label Encoding
Favicon
PHP: Useful Encoding and decoding Functions You Need to Know
Favicon
How good is my video? A (traditional) video quality metrics survey
Favicon
String encodings
Favicon
The unicode encoding system
Favicon
Unicode
Favicon
Serialization
Favicon
Base 64 Encoder-Decoder in Java
Favicon
Windows 系統上 Python 的文字輸出編碼
Favicon
UTF-8 strings in C (3/3)

Featured ones: