- Base64 Encoding and Decoding in Java
- Encoding data into Base64
- Decoding data from Base64
- URL-safe Base64 encoding
- Removing padding characters from Base64 strings
- MIME-compliant Base64 encoding
- Class Base64
- Nested Class Summary
- Method Summary
- Methods declared in class java.lang.Object
- Method Details
- getEncoder
- getUrlEncoder
- getMimeEncoder
- getMimeEncoder
- getDecoder
- getUrlDecoder
- getMimeDecoder
- Base64 Encode decode URL/URI string in Java 8
- Example of Base64 Encoding and decoding of URL:
- JDK classes for Base 64 encoding & decoding:
- Encoder class:
- Decoder class:
- Code: Encode & decode URL using Base64 (Java8)
- Output: Encode & decode of URL/URI (Base64 / Java8)
- You may also like:
Base64 Encoding and Decoding in Java
Base64 is a binary-to-text encoding scheme that takes raw input bytes and encodes it in a radix-64 representation, using the characters A-Z, a-z, 0-9, and the symbols + , / and = . Base64 encoding is used when there is a need to store or transmit binary data over a system that only supports text data, without losing or corrupting said data.
Java 8 provides a Base64 class in the java.util package which can be used to encode strings into Base64, and decode Base64 strings into normal strings.
Encoding data into Base64
To encode a string into Base64, first, we convert the string into a byte array. Then, we fetch an object of the Base64.Encoder class using Base64.getEncoder() , and pass the byte array to the encodeToString() method:
String inputString = "hello world~"; byte[] inputStringBytes = inputString.getBytes(); String base64String = Base64.getEncoder().encodeToString(inputStringBytes); System.out.println(base64String); // prints: aGVsbG8gd29ybGR+
We can also perform the entire conversion within a single expression, as shown below:
String inputString = "hello world~"; String base64String = Base64.getEncoder().encodeToString(inputString.getBytes()); System.out.println(base64String); // prints: aGVsbG8gd29ybGR+
Decoding data from Base64
To decode a Base64-encoded string, we use Base64.getDecoder() to fetch an object of the Base64.Decoder class, and pass the input string to its decode() method. This gives us a byte array, which can be converted to a string.
String inputString = "aGVsbG8gd29ybGR+"; byte[] base64DecodedBytes = Base64.getDecoder().decode(inputString); String decodedString = new String(base64DecodedBytes); System.out.println(decodedString); // prints: hello world~
Just as before, we can perform the conversion within a single expression:
String inputString = "aGVsbG8gd29ybGR+"; String decodedString = new String(Base64.getDecoder().decode(inputString)); System.out.println(decodedString); // prints: hello world~
URL-safe Base64 encoding
Base64 strings make use of the symbols + , / . However, these characters carry special meaning when used in the context of a filename or an URL. For example, in the context of a filename, a string such as AB/CD implies there’s a folder named AB in which there’s a file named CD . Java supports a URL/filename-safe Base64 encoding scheme, where the + and / characters are replaced with — and _ characters to prevent such conflicts.
We can fetch the URL-safe encoder using Base64.getUrlEncoder() . Then, we can convert our input string to Base64 using the encodeToString() method:
String inputString = "hello world~"; String base64String = Base64.getUrlEncoder().encodeToString(inputString.getBytes()); System.out.println(base64String); // prints: aGVsbG8gd29ybGR-
Similarly, we can fetch the URL-safe decoder using Base64.getUrlDecoder() method, and we can convert a Base64-encoded string using the decode() method like so:
String inputString = "aGVsbG8gd29ybGR-"; String decodedString = new String(Base64.getUrlDecoder().decode(inputString)); System.out.println(decodedString); // prints: hello world~
Removing padding characters from Base64 strings
When a string is converted to Base64, the padding character = is added to make the length of the output string divisible by 3. This behavior is to achieve technical compliance with the Base64 specification. However, the padding is optional and even when a Base64-encoded string does not have padding, it can be successfully decoded.
To convert a string to Base64 without padding characters, we can use the withoutPadding() method of the Base64.Encoder class, as shown below:
String inputString = "this is a string"; String base64String = Base64.getEncoder().withoutPadding().encodeToString(inputString.getBytes()); System.out.println(base64String); // prints: dGhpcyBpcyBhIHN0cmluZw
Had we not used the withoutPadding() option, we would have got the string dGhpcyBpcyBhIHN0cmluZw== as the output.
There are no special steps required for decoding the Base64-encoded string without padding. It can be decoded using the regular Base64 decoder, like so:
String inputString = "dGhpcyBpcyBhIHN0cmluZw"; String decodedString = new String(Base64.getUrlDecoder().decode(inputString)); System.out.println(decodedString); // prints: this is a string
MIME-compliant Base64 encoding
MIME is a specification to transmit non-ASCII data to email servers. MIME encoding is similar to Base64 encoding, however, if the Base64-encoded data has more than 76 characters, the data is split on to multiple lines, each line having a maximum of 76 characters.
To convert a string into a MIME-compliant Base64 encoding, we can fetch the MIME-compliant encoder by using Base64.getMimeEncoder() , and then pass the input string to the encodeToString() method:
String inputString = "The quick brown fox jumps over the lazy dog. " + "Jackdaws love my big sphinx of quartz. " + "Pack my box with five dozen liquor jugs."; String base64String = Base64.getMimeEncoder().encodeToString(inputString.getBytes()); System.out.println(base64String);
The program produces the following output. Since there are 172 characters in the output, it has been split on to 3 lines.
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gSmFja2Rhd3MgbG92 ZSBteSBiaWcgc3BoaW54IG9mIHF1YXJ0ei4gUGFjayBteSBib3ggd2l0aCBmaXZlIGRvemVuIGxp cXVvciBqdWdzLg==
We can decode a MIME-compliant Base64 string by fetching the MIME-compliant decoder with Base64.getMimeDecoder() :
String inputString = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4gSmFja2Rhd3MgbG92\r\n" + "ZSBteSBiaWcgc3BoaW54IG9mIHF1YXJ0ei4gUGFjayBteSBib3ggd2l0aCBmaXZlIGRvemVuIGxp\r\n" + "cXVvciBqdWdzLg=="; String decodedString = new String(Base64.getMimeDecoder().decode(inputString)); System.out.println(decodedString);
Running the program produces the original input string:
The quick brown fox jumps over the lazy dog. Jackdaws love my big sphinx of quartz. Pack my box with five dozen liquor jugs.
Class Base64
Unless otherwise noted, passing a null argument to a method of this class will cause a NullPointerException to be thrown.
Nested Class Summary
This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
Method Summary
Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
Methods declared in class java.lang.Object
Method Details
getEncoder
getUrlEncoder
getMimeEncoder
getMimeEncoder
Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
getDecoder
getUrlDecoder
getMimeDecoder
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Base64 Encode decode URL/URI string in Java 8
Given URL (Uniform Resource Locator) or URI, we would like to encode URL to Base64 and decode Base64 encoded back to original URL 0r URL.
Example of Base64 Encoding and decoding of URL:
- URL = “https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html”.
- Base64 encoded URL should: “aHR0cHM6Ly9kb2NzLm9yYWNsZS5jb20vamF2YXNlLzgvZG9jcy9hcGkvamF2YS91dGlsL0Jhc2U2NC5EZWNvZGVyLmh0bWw=”
- If decode Base64 encoded URL (Step 2), then we would get original URL back (Step number 1) “https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html”
JDK classes for Base 64 encoding & decoding:
- Base64 class has two inner classes Encoder and Decoder which are used to encode and decode input byte data respectively.
- We have discussed the Base64 encoding and decoding of Stringwith and withoutPadding using Encoder and Decoder classes.
Encoder class:
- Encoder class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
- Instances of Base64.Encoder class are SAFE for use by multiple concurrent threads.
Decoder class:
- Decoder class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
- Instances of Base64.Decoder class are SAFEfor use by multiple concurrent threads.
Code: Encode & decode URL using Base64 (Java8)
package org.learn; import java.util.Base64; public class Base64URLEncodeDemo < public static void main(String[] args) < String input = "https://docs.oracle.com/javase/8/docs/api/java/" + "util/Base64.Decoder.html"; byte[] bytesInput = input.getBytes(); System.out.println("URL : "+input); //Encode URL to Base64 Base64.Encoder base64Encoder = Base64.getUrlEncoder(); String encodedString = base64Encoder.encodeToString(bytesInput); System.out.println("Encoded URL :" + encodedString); //Decode Base64 encoded URL to URL Base64.Decoder base64Decoder = Base64.getUrlDecoder(); byte[] byteDecoded = base64Decoder.decode(encodedString); System.out.println("Decoded URL :" + new String(byteDecoded)); >>
Output: Encode & decode of URL/URI (Base64 / Java8)
URL : https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html Encoded URL :aHR0cHM6Ly9kb2NzLm9yYWNsZS5jb20vamF2YXNlLzgvZG9jcy9hcGkvamF2YS91dGlsL0Jhc2U2NC5EZWNvZGVyLmh0bWw= Decoded URL :https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Decoder.html