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
Sample String: India Team will win the Cup Encoded String: SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
Program 2: Decode Basic Base 64 format to String
Encoded String: SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw actual String: India Team will win the Cup
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- URL Encoding/Decoding using Base64 in Java
- Huffman Decoding
- Run Length Decoding in Linked List
- Primitive data type vs. Object data type in Java with Examples
- Encoding a word into Pig Latin
- Encryption vs Encoding vs Hashing
- Program to implement Run Length Encoding using Linked Lists
- Basic Operators in Java
- ProcessBuilder in Java to create a basic online Judge
- Java Basic Syntax
- Comparison of boolean data type in C++ and Java
- How to Create a Basic Color Picker Tool in Android?
- Type conversion in Java with Examples
- Is an array a primitive type or an object in Java?
- Type Erasure in Java
- Local Variable Type Inference or LVTI in Java 10
- How to create a Java HashMap of user defined class type?
- FileStore type() method in Java with Examples
- Finding Data Type of User Input using Regular Expression in Java
- Function overloading and return type
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.