Open your browser console and run this:
atob("aGVsbG8="); // "hello" No key, no password, nothing else involved. Base64 is a way of writing bytes as text, and anyone can turn it back. People still mix it up with encryption, and I can see why: a string like dXNlcjpwYXNz looks like gibberish at first glance.
What Base64 does
It takes bytes and writes them using 64 characters: A-Z, a-z, 0-9, + and /. Every 3 bytes become 4 characters, so the output ends up about a third larger. The reason to use it is practical. Some places only accept text, like JSON, headers and URLs, and Base64 lets binary data fit.
The spec, RFC 4648, says it plainly: Base64 "does not provide any computational confidentiality."
// Browser btoa("hello"); // "aGVsbG8=" atob("aGVsbG8="); // "hello" // Node Buffer.from("hello").toString("base64"); // "aGVsbG8=" Buffer.from("aGVsbG8=", "base64").toString(); // "hello" Where you'll meet it
Basic auth sends a header like this:
Authorization: Basic dXNlcjpwYXNz Decode the part after Basic and you get user:pass. That's the real username and password in a different format, so anyone who can see the request can read them.
JWTs are three Base64URL chunks separated by dots. Base64URL is the same idea with - and _ in place of + and /. The middle chunk is the payload, and it's just JSON:
function decodeBase64Url(part) { const b64 = part.replace(/-/g, "+").replace(/_/g, "/"); return atob(b64); } const payload = "eyJzdWIiOiIxMjM0IiwibmFtZSI6IkFkYSIsInJvbGUiOiJhZG1pbiJ9"; JSON.parse(decodeBase64Url(payload)); // { sub: "1234", name: "Ada", role: "admin" } The last chunk is a signature. If someone edits the payload, the signature stops matching and the server can tell. It doesn't hide the payload from anyone, though, so keep secrets out of it.
One gotcha with Unicode
btoa only accepts characters up to U+00FF, so it throws on plenty of ordinary text:
btoa("é"); // "6Q==" btoa("€"); // InvalidCharacterError Encode the text to UTF-8 bytes first, then Base64 the bytes:
const bytes = new TextEncoder().encode("€"); const b64 = btoa(String.fromCharCode(...bytes)); // "4oKs" // and back const bin = atob("4oKs"); new TextDecoder().decode(Uint8Array.from(bin, c => c.charCodeAt(0))); // "€" Encoding, encryption and hashing
These three get confused a lot. Encoding (Base64, URL encoding) changes the format, and since no key is involved, anyone can reverse it. Encryption hides the data from anyone without the key. Hashing produces a fixed-size fingerprint that can't be turned back, and you compare fingerprints instead of reading the original.
If you can get the original back without a secret, nothing is being protected.
I wrote a longer version that also covers signing and where random values fit: encoding vs encryption vs hashing.
