Understanding Base64 Encoding, in Plain English for Developers
Base64 turns bytes into text. It uses 64 characters, pads with equals signs, and makes files larger. It does not encrypt or compress them.
By Rehan Baqir
Learn more
Base64 translates arbitrary bytes into 64 ASCII characters so binary can sit inside JSON, email MIME headers, and data URLs without getting corrupted. Encryption, compression, and integrity checks are separate jobs. Anyone can reverse Base64, because there is no key. Every 3 input bytes become 4 characters, so the text is about 33% larger than the binary. Try a sample in the Base64 encoder and the Base64 decoder. Secrets need TLS and a real hash. The hash generator is the on-site tool for that fingerprint. Percent-escaping a URL is a different job, handled by the URL encoder. When the decoded payload is JSON, the JSON formatter makes a clipped string easier to spot.
Developers still find codebases where an API token or a password was "hidden" with Base64. Anyone can paste that string into a decoder and read it.
The problem Base64 solves
Computers store two broad kinds of data: text and binary. Text fits in JSON, XML, email bodies, and log files. Binary, such as image bytes, PDF chunks, and compressed archives, does not always fit cleanly.
Binary data can include null bytes, control characters, and sequences that break parsers. Email systems from the 1990s needed a way to attach photos without corrupting the message. Base64 was the answer: translate every chunk of binary into printable ASCII that survives a text-only channel.
It is binary written in an alphabet those systems already accept.
How the encoding works
Base64 reads input in groups of 3 bytes (24 bits) and splits those 24 bits into 4 groups of 6 bits. Each 6-bit value maps to one character from a 64-character alphabet:
A–Z → 0–25
a–z → 26–51
0–9 → 52–61
+ → 62
/ → 63
The name comes from those 64 printable characters.
Example: encoding the string Hi.
His 72 andiis 105 in ASCII- Two bytes are 16 bits. Base64 pads the group out to 3 bytes with a zero byte
- Those 24 bits become four 6-bit indices
- The output is
SGk=. The=is padding, covered below
You do not need to do this by hand. The point of the walkthrough is that Base64 is deterministic math. The same input produces the same output every time.
Use Base64 Encode to paste a small sample and watch the output change when you edit one character.
Padding with equals signs
The input length has to be divisible by 3 bytes. When it is not, the encoder adds padding:
| Input bytes mod 3 | Padding added |
|-------------------|---------------|
| 0 | none |
| 1 | == |
| 2 | = |
Decoders expect that padding. Strip it carelessly and some strict parsers fail. If you copied Base64 from a log and the trailing = was cut off, restore padding until the string length is a multiple of 4.
Base64 Decode is how you check whether a payload survived copy-paste.
Why the output is longer
Three raw bytes (24 bits) become four characters. That is a 4/3 ratio, about 33% larger than the original binary.
Base64 will not shrink a file. Compression is a different job. How PDF compression works removes redundant image data. Base64 adds characters so a text system can carry binary.
Encoding and encryption are different jobs
- Encoding converts the format. It is reversible by design. No key is required
- Encryption requires a secret key. The point is to block unauthorized reading
- Hashing is a one-way fingerprint. See the Hash Generator
JWT payloads are Base64-encoded JSON. Anyone can decode the middle segment and read the claims. The signature, the third segment, provides integrity. It does not keep the payload secret.
For passwords, read Password Security Guide. Encoding a password before a POST, even over HTTPS, still sends recoverable text to anyone who intercepts the request and decodes it.
Text in Base64: the UTF-8 step
Encoding a string such as café needs a step people skip:
- Encode the string as UTF-8 bytes, not as ASCII code units alone
- Base64 those bytes
Decoding reverses the path: Base64, then bytes, then a UTF-8 string.
Skip UTF-8 and emoji, accented characters, and CJK text come out wrong. Test with non-English samples whenever you build a feature that handles more than English.
Base64url
Standard Base64 uses + and /, which already mean something in URLs. Base64url swaps them:
+becomes-/becomes_- The padding
=is often omitted
You will see Base64url in JWTs, in some API tokens, and in filename-safe encodings. A decoder may need the padding restored before it can process the string.
This is different from URL encoding, which percent-escapes individual characters (%20 for a space). Base64url is still an alphabet swap.
When Base64 makes sense
Good fits:
- A tiny icon embedded in HTML or CSS as a data URL
- Binary inside JSON when the API contract requires a text-only body
- Email MIME attachments in older systems
- A small binary blob in a database that prefers text columns
Poor fits:
- A large file transfer. Use a multipart upload or a signed object-storage URL
- "Protecting" credentials or other personal data
- A stand-in for a real file-attachment endpoint in a modern REST API
Base64 in web development covers data URLs, API payloads, and the mistakes that show up in that setting.
Debugging malformed Base64
| Symptom | Likely cause |
|---------|--------------|
| Decode throws "invalid character" | Smart quotes, line breaks in a PEM block, or a Base64url string fed to a standard decoder |
| Wrong output bytes | Decoded as Latin-1 instead of UTF-8 |
| Truncated string | A chat tool or an email client clipped a long line |
| Padding error | A missing trailing = |
Pipe decoded JSON through JSON Formatter when the payload nests structured data. Truncation is easier to see there than in raw bytes.
Base64 in the wild
You will run into Base64 when:
- An API returns
"content": "JVBERi0xLjQK..."for an inline PDF - DevTools shows
data:image/png;base64,iVBORw0KG... - An OAuth flow passes a state parameter
- A certificate file uses PEM, Base64 between header lines
The pattern is an alphanumeric block with an optional = at the end. Recognizing it speeds up debugging.
Security notes for production
- Cap the input length. Decoding an attacker-controlled multi-megabyte string can exhaust memory
- Validate before you decode. Reject characters outside the alphabet early
- SVG inside a data URL can carry script if sanitization fails. Treat it as active content
- Do not log decoded secrets. Decoding makes the bytes readable, including a password someone encoded by mistake
Limitations
The size overhead is about 33%. Encoding a 15 MB PDF or video into a Base64 JSON field balloons the payload to about 20 MB and spikes parser memory. Use a binary multipart upload or a signed upload URL for large files.
Base64 offers no confidentiality. Pair transport with TLS, and use encryption or hashing (Hash Generator) when you are protecting a secret.
Related articles
- Base64 in Web Development: practical web use and the pitfalls
- Common JSON Formatting Errors: when a Base64 field breaks a parser
- Password Security Guide: what actually protects a credential
Conclusion
Base64 is a translation layer. Binary goes in, ASCII text comes out, larger and portable. Confidentiality, smaller files, and proof that the data is authentic come from other tools. Remember 3 bytes to 4 characters, and encode text as UTF-8 first. The odd strings in a log are usually payloads you can decode.
Frequently Asked Questions
Common questions answered to help you get the most from this tool.