Base64 Encoding Reference
Base64 represents arbitrary bytes using printable characters. It improves transport compatibility but increases size and provides no security.
How Base64 maps bytes
Base64 processes input in three-byte groups and represents their bits as four symbols from a 64-character alphabet. If the last group has fewer than three bytes, the standard form uses one or two equals signs as padding. The encoded size is roughly four thirds of the original before line wrapping or surrounding JSON, HTML, and protocol overhead.
| Input | Standard Base64 |
|---|---|
| f | Zg== |
| fo | Zm8= |
| foo | Zm9v |
| Hello | SGVsbG8= |
Text must become bytes first
Base64 encodes bytes, not abstract characters. Convert text to a documented character encoding, normally UTF-8, before encoding. A legacy browser helper that assumes one-byte characters can fail on emoji or non-Latin text. On decode, recover bytes first and interpret them with the same character encoding only when the payload is actually text.
Standard versus URL-safe alphabets
| Variant | Characters 62 and 63 | Padding |
|---|---|---|
| Standard Base64 | + and / | Normally present where required |
| Base64URL | - and _ | Often omitted by protocol convention |
JWT compact parts use Base64URL without padding. Do not decode them with a strict standard-Base64 parser unless the implementation explicitly handles alphabet replacement and missing padding. Follow the containing protocol rather than guessing a variant from one sample.
Whitespace and MIME wrapping
Email and PEM-style formats can wrap encoded data across lines, while many API fields expect one uninterrupted value. Some decoders ignore whitespace and others reject it. Preserve the required wrapping for the target format. A PEM document also includes label lines that are not part of the Base64 data itself.
Data URLs and JSON
A data URL includes metadata and a comma before the encoded payload, for example data:image/png;base64,.... Decode only the part after the comma under a trusted size and type policy. JSON has no binary type, so APIs sometimes use Base64 strings, but large files are usually more efficient as multipart uploads or binary responses.
Security and validation
Base64 is reversible and does not hide API keys, passwords, or personal data. Add authenticated encryption or a signature when the protocol requires confidentiality or integrity. Bound decoded size before allocation, reject unexpected alphabet or padding, and validate the resulting file format. Never trust a Base64 string merely because decoding succeeds.
Frequently Asked Questions
Is Base64 encryption?
No. It provides no confidentiality or integrity.
How does Base64URL differ?
It uses - and _ and commonly omits padding.
Why are equals signs added?
They pad an incomplete final three-byte group.