Base64 in Java
How to encode and decode Base64 in Java — with copy-paste examples and a live converter to check your output.
Since Java 8, java.util.Base64 provides standard, URL-safe and MIME encoders/decoders. Always pass an explicit charset when converting between String and byte[].
Encode to Base64 in Java
import java.util.Base64;
import java.nio.charset.StandardCharsets;
String encoded = Base64.getEncoder()
.encodeToString("Hello, World!".getBytes(StandardCharsets.UTF_8));Decode Base64 in Java
import java.util.Base64;
import java.nio.charset.StandardCharsets;
byte[] decoded = Base64.getDecoder().decode(encoded);
String text = new String(decoded, StandardCharsets.UTF_8);Notes & gotchas
Use Base64.getUrlEncoder() for URL-safe output and Base64.getMimeEncoder() for MIME line-wrapped output. Specifying StandardCharsets.UTF_8 avoids platform-dependent encoding bugs.
Try it live
Base64 in Java — FAQ
How do I Base64 encode a string in Java?+
Use the code shown above. Use Base64.getUrlEncoder() for URL-safe output and Base64.getMimeEncoder() for MIME line-wrapped output. Specifying StandardCharsets.UTF_8 avoids platform-dependent encoding bugs.
How do I decode Base64 back to text in Java?+
Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.
Is Base64 encoding the same across programming languages?+
Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.
Does Base64 secure my data?+
No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.