Why Base64 exists
Many older protocols and formats, such as email and URLs, were designed for text and can mangle arbitrary bytes. Binary data such as an image or a file can contain bytes that look like control characters or line endings. Base64 solves this by representing any data using only letters, digits, plus and slash: 26 uppercase, 26 lowercase, 10 digits and 2 symbols make 64 characters.
You see it whenever binary data must live inside text: images embedded in HTML or CSS as data URLs, attachments in emails, keys and certificates in PEM files, and the pieces of a JSON Web Token.
How it works, in steps
- Take the input as bytes and group them in threes. Three bytes are 24 bits.
- Split those 24 bits into four groups of 6 bits. Each 6-bit value is a number from 0 to 63.
- Look up each number in the Base64 alphabet (A–Z, a–z, 0–9, +, /).
- If the last group has fewer than three bytes, add = signs at the end as padding so the output length is a multiple of four.
Example: the three letters "Man" are bytes 77, 97, 110. They form the 6-bit groups 19, 22, 5 and 46, which map to T, W, F and u. So "Man" encodes to TWFu.
Padding and size
Because three bytes become four characters, Base64 makes data about 33% larger. A 3,000-byte file becomes 4,000 characters, and 1,000 bytes become 1,336 characters, since the last group is padded.
The = padding tells a decoder how many bytes the last group held. "Ma" (two bytes) becomes TWE= and "M" (one byte) becomes TQ==. A longer example: "Hello" is SGVsbG8= and "Hello, World!" is SGVsbG8sIFdvcmxkIQ==.
| Input | Base64 |
|---|---|
| M | TQ== |
| Ma | TWE= |
| Man | TWFu |
| Hello | SGVsbG8= |
| Hello, World! | SGVsbG8sIFdvcmxkIQ== |
| café (UTF-8) | Y2Fmw6k= |
Variants: standard and URL-safe
The standard alphabet uses + and /, which have special meanings in URLs and file names. The URL-safe variant, called Base64URL, uses - and _ instead and often leaves off the padding. The three bytes 251, 255, 254 encode to +//+ in standard Base64 and -__- in the URL-safe form. JSON Web Tokens use the URL-safe version.
Base64 is not encryption
Anyone can decode Base64 instantly, with no key. It hides nothing. Basic authentication headers in HTTP use Base64 only to carry the username and password as text, which is why they must always be sent over HTTPS. If you need secrecy, use real encryption; if you need to check integrity, use a hash or a signature.
The same applies to text with accents or other non-English characters: encode it to bytes first (usually UTF-8) and then to Base64, and decode in the reverse order. Skipping that step is the most common source of garbled results.
When to use it, and when not to
- Good uses: small images inline in CSS or HTML, carrying binary data in JSON, email attachments, and tokens.
- Poor uses: large images on a web page, where the 33% overhead and the loss of caching make a normal file better.
- Never use it to "protect" passwords or secrets.
- Prefer well-tested library functions over writing your own encoder.
Frequently asked questions
Is Base64 secure?
No. It is a reversible encoding that anyone can decode without a key. It is for compatibility, not for secrecy.
Why does Base64 end with equals signs?
They are padding. Base64 works in groups of three bytes, and when the input length is not a multiple of three, = characters fill the last group so the output length stays a multiple of four.
Why is the Base64 version longer?
Every 3 bytes become 4 characters, so the output is about one third larger than the input.
Can I Base64 encode an image?
Yes. The result can be used in a data URL such as data:image/png;base64,... which is handy for tiny icons, but for large images a normal file is more efficient.
Try these tools
Base64 Encode & Decode
Encode text to Base64 or decode Base64 back to text. Supports UTF-8 and URL-safe Base64. Free, instant and runs in your browser.
Open toolImage to Base64
Convert an image to a Base64 string or data URL to embed in HTML, CSS or JSON. Copy it, see the size overhead, and keep your file private in the browser.
Open toolBase64 to Image
Paste a Base64 string or data URL and see the decoded image instantly. The file type is detected from its bytes, and you can download it. Private, no upload.
Open toolJWT Decoder
Decode a JWT to read its header, payload and expiry time. Free and runs locally, so your token is never sent to a server.
Open toolURL Encode & Decode
Encode or decode URLs and query strings with percent-encoding. Choose component or full-URL mode. Free, instant and private.
Open toolKeep reading
How to verify web design files
How to verify web design files before you accept, publish or hand them off: check they arrived intact, nothing is missing, the code is valid, images are right and the build matches the design.
Read guideHow JSON Web Tokens work, and how to read one safely
Understand the three parts of a JSON Web Token, why the payload is readable by anyone, what the signature actually protects, and how to inspect a token without leaking it.
Read guideUnix timestamps explained: seconds, milliseconds and the Year 2038 problem
Learn what a Unix timestamp is, how to tell seconds from milliseconds, how to convert one to a human date, why time zones matter, and what the Year 2038 problem is.
Read guide