Base64 is not encryption: what it does, and the bugs it causes

Try it in the tool

Base64 Encode / Decode

Encode and decode Base64 with proper UTF-8 and URL-safe support.

Hash Generator

MD5, SHA-1, SHA-256/384/512 and HMAC — computed locally.

JWT Decoder

Decode JWTs, check expiry and verify HMAC signatures — privately.

A developer finds a long string such as YWRtaW46czNjcmV0 in a config file and relaxes: it looks scrambled, so it must be protected. It is not. Base64 is the way text-only systems carry bytes, and it hides nothing. This article explains what it really does, why that matters for security, and the handful of bugs that account for most “my Base64 is broken” questions.

What Base64 does

Computers move bytes. Some channels, such as JSON, XML, email bodies, URLs and HTTP headers, only carry text. Base64 bridges the gap: it takes three bytes (24 bits), cuts them into four groups of six bits, and writes each group as one of 64 characters: A–Z, a–z, 0–9, + and /. The reverse is just as mechanical. Try it in the Base64 tool:

Input Base64 Why
abc YWJj 3 bytes fit exactly, 4 characters
ab YWI= 2 bytes, one = of padding
a YQ== 1 byte, two = of padding
abcd YWJjZA== 4 bytes: one full group plus a short one

Every three bytes become four characters, so Base64 output is about a third larger than its input. A 1 MiB file becomes 1,398,104 characters, a growth of 33%. That is the price of staying inside a text-only channel, and the reason you should not Base64 large files into JSON when a separate upload would do.

Why it is not security

Base64 has no key. The mapping is published in RFC 4648, so every programming language ships a decoder. Two everyday places show the consequence.

HTTP Basic authentication sends Authorization: Basic YWRtaW46czNjcmV0. That value is Base64 of user:password. Decode it in the tool and you get admin:s3cret straight away. Basic auth is only acceptable over HTTPS, because the header is readable by anything that sees the request.

Kubernetes Secrets store their values in a data: field as Base64. A line such as password: cGFzc3dvcmQ= decodes to password. The encoding exists so binary values can sit in YAML, not to protect them. What protects a Secret is access control and encryption at rest in the cluster, not the way the value is written.

If you need to keep something confidential, use encryption with a key that you control, or do not store the value where untrusted people can read it.

Encoding, hashing and encryption are three different things

People mix these up because all three produce unreadable-looking text.

Reversible? Needs a key? Purpose
Encoding (Base64) Yes, by anyone No Carry bytes through a text channel
Hashing (SHA-256) No No Fingerprint data, verify integrity
Encryption (AES) Yes, with the key Yes Keep data confidential

The hash generator shows the difference. The SHA-256 of hello is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 in hex. Switch the output to Base64 and the same hash reads LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=. Only the notation changed. The hash itself cannot be turned back into hello, while the Base64 of hello (aGVsbG8=) can.

Two practical rules follow. Never store passwords as Base64, because that is just plain text with extra steps. And do not store them as a plain fast hash either: use a function built for the job, such as Argon2id, bcrypt or scrypt. MD5 is fine as a checksum, but the tool warns that it is broken for security.

Bug 1: UTF-8 versus Latin-1

The most common real bug is text that does not survive the round trip. Base64 encodes bytes, so the first question is always which bytes. The word café is five bytes in UTF-8 (the é takes two) and gives Y2Fmw6k=. JavaScript’s btoa('café') gives Y2Fm6Q== instead, because btoa treats each character as a single byte, which is Latin-1. Both are valid Base64, and they decode to different bytes.

The mismatch gets worse outside Latin-1. btoa('東京') does not return a wrong answer; it throws InvalidCharacterError. UTF-8 handles it without trouble: 東京 is six bytes and gives 5p2x5Lqs. The fix is to convert text to bytes with an explicit encoding, new TextEncoder().encode(text), before encoding, and to decode with TextDecoder afterwards. The Base64 tool does this for you and always uses UTF-8.

Bug 2: the URL-safe alphabet

Standard Base64 uses + and /, and both are special in URLs: + can turn into a space and / splits a path. The URL-safe variant swaps them for - and _. Take the text ???>>>: standard Base64 gives Pz8/Pj4+, URL-safe gives Pz8_Pj4-. Same bytes, different strings.

Mixing the two is a classic cause of “invalid character” errors. A decoder expecting the standard alphabet rejects - and _, and one expecting URL-safe rejects + and /. The URL-safe form also usually drops the trailing =, which is how JWTs are written. Without padding, the tool still decodes YQ as a and YWI as ab. For the details of how that affects tokens, see decoding a token is not verifying it, and open any token in the JWT decoder to see the segments decoded.

Bug 3: binary data is not text

Decode a Base64 string that holds an image and the result is not a sentence. /9j/4AAQSkZJRg== is the beginning of a JPEG file: the bytes ff d8 ff e0 followed by the letters JFIF. A tool that assumes text would print garbage. The Base64 tool notices the bytes are not valid UTF-8 and shows a hex dump with a warning instead. If you decode to a file and the content looks like noise, the data was probably binary all along.

Data URIs, line breaks and errors

Base64 often arrives wrapped in other formats:

  • A data URI such as data:text/plain;base64,SGVsbG8= embeds content in a page or stylesheet. The tool drops the data:...;base64, prefix and decodes Hello.
  • MIME email wraps Base64 at 76 characters per line. Line breaks are not part of the data, and the tool ignores them: SGVsbG8s and IHdvcmxk on two lines decode to Hello, world.
  • Invalid input gets a precise message. YWJj$ fails with Invalid Base64 character '$' at position 5, and YWJjZ fails because the length is truncated or has an extra character.

When Base64 is the right tool

Use it when a text-only channel has to carry bytes: embedding a small image in CSS, putting a binary value in JSON, sending an attachment in email, or sending credentials in an HTTPS header. Avoid it for anything you want to protect, for large files where a separate upload is cheaper, and for passwords in any form.

Checklist when a Base64 value looks wrong

  1. Is it encoding at all, or is it supposed to be encrypted or hashed? A reversible string is only encoded.
  2. Which alphabet does the producer use: standard or URL-safe? Is the padding present?
  3. Are the bytes UTF-8? If accented characters turn into two odd ones, a Latin-1 step is hiding in the pipeline.
  4. Is the decoded result binary? A hex dump means it was never text.
  5. Are line breaks or a data: prefix confusing the decoder?

Everything you paste into these tools is processed in your browser tab and never uploaded, but treat real credentials as sensitive anyway.

Frequently asked questions

Is Base64 a form of encryption?

No. Base64 is an encoding: a fixed, public rule that maps bytes to 64 printable characters and back. There is no key and no secret, so anyone can reverse it in a second. If a value must stay confidential, it needs real encryption with a key you keep, or it should not be sent at all.

Why does my Base64 string end with = or ==?

Base64 reads three bytes at a time and writes four characters. When the input does not divide by three, the output is padded with = so the length is a multiple of four: one leftover byte gives ==, two leftover bytes give =. The padding carries no data. Base64URL, used in JWTs, usually drops it, and most decoders accept both.

Why does decoded text show garbage like à or á?

The bytes were decoded as Latin-1 (or Windows-1252) instead of UTF-8. A character such as é is two bytes in UTF-8 and one in Latin-1, so reading UTF-8 bytes as Latin-1 shows two wrong characters. In JavaScript, atob returns a string of byte values, so you must run it through TextDecoder to get UTF-8 text.

Is it safe to hide an API key in front-end code with Base64?

No. Anything that reaches the browser can be read by the user, and Base64 is undone in one step, so the key is exposed exactly as if it were written in plain text. Keep secrets on a server and give the browser a short-lived token with only the permissions it needs.

What is the difference between Base64 and Base64URL?

Standard Base64 uses + and /, which have special meaning in URLs and file names. Base64URL replaces them with - and _ and often omits the = padding, so the result can sit in a query string, a path or a JWT without extra escaping. Pick the alphabet your consumer expects: the two are not interchangeable byte for byte.

More guides