Base64 Java Encode and Decode a String Last Updated : 31 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Base64 encoding allows encoding binary data as text strings for safe transport, at the cost of larger data size. It is commonly used when there is a need to encode binary data that needs to be stored or transferred via media that are designed to deal with textual data (like transporting images inside an XML, JSON document, etc.). How to Convert a String to Base64 Encoded String in Java?In Java, the java.util.Base64 class provides static methods to encode and decode between binary and base64 formats. The encode() and decode() methods are used. Syntax: String Base64format= Base64.getEncoder().encodeToString("String".getBytes());Example to convert a String to base64 encoded String: Java // Java Program to Convert String // to Base64 Encoded String import java.util.Base64; public class Sample { public static void main(String[] args) { // Input string to be encoded String inputString = "GeeksForGeeks"; // Creating Base64 encoder and decoder instances Base64.Encoder encoder = Base64.getEncoder(); Base64.Decoder decoder = Base64.getDecoder(); // Encoding the input string String encodedString = encoder.encodeToString(inputString.getBytes()); System.out.println("Encoding Done:"); // Displaying the original and encoded strings System.out.println("Original String: " + inputString); System.out.println("Base64 Encoded String: " + encodedString); // Decoding the Base64 encoded string byte[] decodedBytes = decoder.decode(encodedString); String decodedString = new String(decodedBytes); System.out.println("\nDecoding Done:"); // Displaying the Base64 encoded and decoded strings System.out.println("Base64 Encoded String : " + encodedString); System.out.println("Original String: " + decodedString); } } OutputEncoding Done: Original String: GeeksForGeeks Base64 Encoded String: R2Vla3NGb3JHZWVrcw== Decoding Done: Base64 Encoded String : R2Vla3NGb3JHZWVrcw== Original String: GeeksForGeeks Explanation of the above Program:It imports the Base64 class for encoding and decoding. It encodes the input string to Base64 format and stores in a variable.It decodes the Base64 encoded string back to original string.It prints the original, encoded and decoded strings to verify it works.The Base64 class provides easy methods to encode and decode strings in this format. Comment More infoAdvertise with us Next Article How to Convert a String to a Character in Java? P pranay0911 Follow Improve Article Tags : Java Java Programs Java-Strings encoding-decoding Java Examples +1 More Practice Tags : JavaJava-Strings Similar Reads Convert a String to a ByteBuffer in Java In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java. Java Program to Convert St 4 min read Convert a String to Character Array in Java Converting a string to a character array is a common operation in Java. We convert string to char array in Java mostly for string manipulation, iteration, or other processing of characters. In this article, we will learn various ways to convert a string into a character array in Java with examples.E 2 min read How to Convert a String to a Character in Java? String and char are fundamental and most commonly used datatypes in Java. A String is not a primitive data type like char. To convert a String to char in Java, we have to perform character-based operations or have to process individual characters.In this article, we will learn how to convert a Strin 3 min read What is Java AES Encryption and Decryption? Java has introduced a new approach in the technology sector as a programming language. Java takes the top spot of technologies used for coding. A Java application design firm can do everything from comprehensive business software to apps for mobile phones and wireless devices. Omnipresent of this So 5 min read Java String Exercise String in Java are the objects which can store characters of values in Java, it act the same as an array of characters in Java. Java String is one of the most important topics in Java programming. It is widely used to manipulate and process textual data. In this article, we will learn about Java Str 5 min read How to Convert a String to Specific Character Encoding in Java? In Java, the process of converting a String to a certain character encoding is converting the string's character sequence to a byte array and applying the chosen character encoding. Java Program to Convert a String to a Specific Character EncodingUsing the String class's getBytes() function to trans 2 min read Like