As of Java 8, there is an officially supported API for Base64 encoding and decoding. In time this will probably become the default choice.

The API includes the class java.util.Base64 and its nested classes. It supports three different flavors: basic, URL safe, and MIME.

Sample code using the "basic" encoding:

import java.util.Base64;

byte[] bytes = "Hello, World!".getBytes("UTF-8");
String encoded = Base64.getEncoder().encodeToString(bytes);
byte[] decoded = Base64.getDecoder().decode(encoded);
String decodedStr = new String(decoded, StandardCharsets.UTF_8);

The documentation for java.util.Base64 includes several more methods for configuring encoders and decoders, and for using different classes as inputs and outputs (byte arrays, strings, ByteBuffers, java.io streams).

Answer from Andrea on Stack Overflow
🌐
Base64Encode.org
base64encode.org › enc › java
Base64 Encoding of "java" - Online
Meet Base64 Decode and Encode, a simple online tool that does exactly what it says: decodes from Base64 encoding as well as encodes into it quickly and easily.
🌐
Java Guides
javaguides.net › 2020 › 02 › base64-decode-online.html
Base64 Decode Online
August 20, 2023 - ▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube · ▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube · Base64 Decoder is a simple and easy-to-use ...
🌐
FusionAuth
fusionauth.io › docs › dev tools › base64 encoder/decoder
Base64 Encoder/Decoder | FusionAuth Docs
For AI agents: The complete documentation index is available at /docs/llms.txt. A markdown version of this page is available at /docs/dev-tools/base64-encoder-decoder.md. ... Use our online tool to encode and decode Base64 data.
🌐
Online Tools
emn178.github.io › online-tools › base64_decode.html
Base64 Decode - Online Tools
Decode standard Base64, Base64URL, unpadded, IMAP-style, or custom-alphabet data to text or binary bytes online.
🌐
Base64.Guru
base64.guru › home › developers › java
Java Base64 Decode | Java | Developers | Base64
import java.util.Base64; class Base64Decode { public static void main(String[] args) { String b64 = "Z3VydQ=="; byte[] decoder = Base64.getDecoder().decode(b64); String str = new String(decoder); System.out.println(str); //-> "guru" } }
🌐
Softbaba
base64.softbaba.com › java-base64-decode-online.php
Java Base64 Decode Online
Base64Encoder is a simple and easy to use online tool to convert encode any encode any binary or text data in different programming languages like Java, We also have a tool to decode Base64 encoded data back to normal binary data.
🌐
Baeldung
baeldung.com › home › java › java base64 encoding and decoding
Java Base64 Encoding and Decoding | Baeldung
February 18, 2026 - Java supports Base64 encoding and decoding features through the java.util.Base64 utility class.
Find elsewhere
🌐
Base64 Decode
base64decode.org
Base64 Decode and Encode - Online
Decode from Base64 format or encode into it with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
Softbaba
base64.softbaba.com › base64-decode-java-online.php
Base64 Decode Java Online
Online tool for base64 decoding a string. Convert a base64 encoded text into an decoded string or download as a binary file using this free · https://www.convertstring.com/EncodeDecode/Base64Decode · 6 दिस॰ 2017 - Java 8 will be remembered mainly for introducing lambdas, streams, a new date/time model, and Nashorn--but don't miss the new Base64 API.
🌐
Java Guides
javaguides.net › 2020 › 02 › base64-encode-online.html
Base64 Encode Online
August 20, 2023 - This Base64 Encode Online tool processes the data entirely client-side, meaning your data never leaves your browser or gets transmitted over the internet.
🌐
Base64.Guru
base64.guru › converter › decode
Base64 Decode | Base64 Converter | Base64
The “Base64 Decode Online” is a free decoder for decoding online Base64 to text or binary
Top answer
1 of 3
2

btoa is broken and shouldn't be used.

The problem is, bytes aren't characters. Base64 encoding does only one thing. It converts bytes to a stream of characters that survive just about any text-based transport mechanism. And Base64 decoding does that one thing in reverse, it converts such characters into bytes.

And the confusion is, you're printing those bytes as if they are characters. They are not.

You end up with the exact same bytes, but javascript and java disagree on how you're supposed to turn that into an ersatz string because you're trying to print it to a console. That's a mistake - bytes aren't characters. Thus, some sort of charset encoding is being used, and you don't want any of this, because these characters clearly aren't intended to be printed like that.

Javascript sort of half-equates characters and bytes and will freely convert one to the other, picking some random encoding. Oof. Javascript sucks in this regard, it is what it is. The MDN docs on btoa explains why you shouldn't use it. You're running into that problem.

Not entirely sure how you fix it in javascript - but perhaps you don't need it. Java is decoding the bytes perfectly well, as is javascript, but javascript then turns those bytes into characters into some silly fashion and that's causing the problem.

2 of 3
1

What you have there is not a text string at all. The giveaway is the AA's at the beginning. Those map to a number of zero bytes. That doesn't translate to meaningful text in any standard character set.

So what you have there is most likely binary data. Converting it to a string is not going to give you meaningful text.


Now to explain the difference you are seeing between Java and Javascript. It looks to me as if both Java and Javascript are making a "best effort" attempt to convert the binary data as if is was encoded in ISO-8859-1 (aka ISO LATIN-1).

The problem is some of the bytes codes are mapping to unassigned codes.

  • In the Java case those unassigned codes are being mapped to ?, either when the string is created or when it is being output.
  • In the Javascript case, either the unassigned codes are not included in the string, or them are being removed when you attempt to display them.

For the record, this is how an online base64 decoder the above for me:

  ����������������Æû$SF3«àöBâ

The unassigned codes are 0x91 0x82 and 0x93. 0x15 and 0x0B are non-printing control codes.


But the bottom line is that you should not be converting this data into a string in either Java or in Javascript. It should be treated as binary; i.e. an array of byte values.

🌐
DEV Community
dev.to › tooldeck › base64-decode-in-java-getdecoder-geturldecoder-getmimedecoder-and-streaming-3b9o
Base64 Decode in Java: getDecoder, getUrlDecoder, getMimeDecoder, and Streaming - DEV Community
April 9, 2026 - The basic decoder rejects anything outside the Base64 alphabet — and that includes the \r\n line breaks that MIME-encoded content always contains. Email attachments, PEM certificates, and some older API responses wrap Base64 output at 76 characters per line. getMimeDecoder() silently ignores line separators and any character not in the Base64 alphabet, so it handles this out of the box. import java.util.Base64; import java.nio.charset.StandardCharsets; public class MimeDecode { public static void main(String[] args) { // PEM certificate body — line-wrapped at 76 characters String pemBody =
🌐
Medium
medium.com › @rkgupta1298 › base64-encoding-in-java-7ff71ea0cc65
Base64 Encoding and Decoding in JAVA | by coffeebytes | Medium
March 15, 2022 - Base64 encoding and decoding capabilities were added in java8 via the java.util.Base64 utility class.
🌐
GeeksforGeeks
geeksforgeeks.org › java › base64-java-encode-and-decode-a-string
Base64 Java Encode and Decode a String - GeeksforGeeks
July 23, 2025 - In Java, the java.util.Base64 class provides static methods to encode and decode between binary and base64 formats.
🌐
Base64 Encode
base64encode.org
Base64 Encode and Decode - Online
Meet Base64 Decode and Encode, a simple online tool that does exactly what it says: decodes from Base64 encoding as well as encodes into it quickly and easily.
🌐
OneCompiler
onecompiler.com › java › 3y2k3s2hz
Base64 - Java - OneCompiler
import java.util.*; public class Base { public static void main(String[] args) { String sample = "Bishop trash"; String BasicBase64format = Base64.getEncoder() .encodeToString(sample.getBytes()); System.out.println("Encoded String:\n" + BasicBase64format); } } ... Write, Run & Share Java code online using OneCompiler's Java online compiler for free.