Base64 Explained: How It Works, Why Files Grow 33%, and Data URLs
Base64 turns binary data – a photo, a PDF, a signature – into plain text letters. You meet it in email attachments, in data: URLs inside web pages, in APIs that send images as JSON, and sometimes behind upload errors on government portals. It is defined in IETF RFC 4648. This explainer shows exactly how it works, why it makes files about 33% bigger, and how it differs from other "encodings" students often confuse it with.
In short: Base64 takes 3 bytes (24 bits) at a time, splits them into four 6-bit numbers, and writes each as one of 64 safe characters (A–Z, a–z, 0–9, +, /). The output is 4/3 the size of the input, and = pads the last group. It is an encoding, not compression and not encryption.
The alphabet
RFC 4648 section 4 defines the standard alphabet. Values 0–25 are A–Z, 26–51 are a–z, 52–61 are 0–9, 62 is + and 63 is /, with = as padding. Section 5 defines a "URL and filename safe" variant that uses - and _ instead of + and /, because those two characters have special meanings in URLs and file paths.
Worked example: encoding "foo"
| Step | Value |
|---|---|
| Input bytes | f = 0x66, o = 0x6F, o = 0x6F |
| 24 bits | 01100110 01101111 01101111 |
| Split into 6-bit groups | 011001 | 100110 | 111101 | 101111 |
| As numbers | 25 | 38 | 61 | 47 |
| Alphabet lookup | Z | m | 9 | v |
| Output | Zm9v |
This matches the test vectors in RFC 4648 itself, which list BASE64("foo") = "Zm9v" and BASE64("foobar") = "Zm9vYmFy".
Padding: when the input is not a multiple of 3
RFC 4648 covers the three cases for the final group:
- A full 24 bits: no padding.
- Only 8 bits left: two characters plus
==. Example from the RFC: "f" →Zg==. - Only 16 bits left: three characters plus
=. Example: "fo" →Zm8=.
Size: why Base64 makes files about 33% bigger
Every 3 bytes become 4 characters, and each character is stored as one byte. So the size grows by a factor of 4/3, plus up to 2 padding characters. MDN's Base64 glossary entry gives the same arithmetic.
| Original file (example sizes) | Base64 text |
|---|---|
| 15 KB signature JPG | about 20 KB |
| 30 KB photo JPG | about 40 KB |
| 300 KB PDF | about 400 KB |
Size limits on forms apply to the file you choose, not to any Base64 text the website may create internally. If a 48 KB file passes your check but the portal still reports an error, the problem is usually elsewhere: the format, pixel dimensions or file name. See online form upload errors and fixes and KB, MB and pixels.
Decoding: the same steps in reverse
To decode, look up each character's 6-bit value, join the bits, and cut them into bytes again. Take Zg==: Z = 25 = 011001 and g = 32 = 100000, which gives 011001 100000. The two = signs say only one byte was encoded, so keep the first 8 bits, 01100110 = 0x66 = "f", and drop the leftover zero bits. A decoder must reject characters outside the alphabet. RFC 4648 says implementations must reject encoded data containing characters outside the base alphabet unless the specification referring to it says otherwise. That is why a single stray space or line break pasted into a Base64 field can cause a "corrupt file" error.
Recognising file types inside Base64
Because every file format starts with fixed "magic" bytes, the start of a Base64 string often tells you what the file is, even without a file name:
| File type | First bytes | Base64 usually starts with |
|---|---|---|
| JPEG | FF D8 FF | /9j/ |
| PNG | 89 50 4E 47 0D 0A 1A 0A | iVBORw0KGgo |
| %PDF- | JVBERi0 | |
| GIF | GIF89a | R0lGODlh |
This helps when debugging. If an API or form says "invalid image" and the data starts with JVBERi0, a PDF was sent where an image was expected. If a file named .jpg starts with iVBORw0KGgo, it is really a PNG with the wrong extension. That is a common cause of the "invalid file format" errors described in online form upload errors.
Where you see Base64
- Email attachments. Email was designed for text, so attachments are sent Base64-encoded in MIME. Base64 has its name from that MIME transfer encoding.
- Data URLs. RFC 2397 defines
data:[<mediatype>][;base64],<data>, so a small image can be embedded directly in HTML or CSS asdata:image/png;base64,iVBORw0KGgo…. The first charactersiVBORw0KGgoare simply the Base64 form of the PNG signature bytes 89 50 4E 47 0D 0A 1A 0A. See how PNG files are structured. - APIs and JSON. JSON has no binary type, so images are often sent as Base64 strings.
- In-browser tools.
FileReader.readAsDataURL()returns a file as a Base64 data URL. How browser-side file processing works explains when tools use this and when they use faster binary methods.
Base64 is not compression, and not encryption
| Term | Purpose | Reversible by anyone? | Changes size |
|---|---|---|---|
| Encoding (Base64, UTF-8) | Represent data in a different form | Yes, no key needed | Base64: +33% |
| Compression (DEFLATE, JPEG) | Make data smaller | Lossless: yes. Lossy: approximately | Smaller |
| Encryption (AES) | Keep data secret | Only with the key | About the same |
| Hashing (SHA-256) | Fingerprint and integrity check | No (one-way) | Fixed-length output |
Anyone can decode Base64 instantly. Never treat a Base64 string of an Aadhaar card, password or certificate as "hidden".
Exercise
- In a browser console, run
btoa("foo")→ "Zm9v" andatob("Zm9vYmFy")→ "foobar". - Note that
btoaworks on single-byte strings only. For Hindi text, first encode to UTF-8 bytes withTextEncoder(see Unicode and UTF-8 for Hindi). - Take a 30 KB JPG, convert it to a data URL and check the length of the string: about 40,000 characters.
Frequently asked questions
Does Base64 compress a file?
No. It makes data about 33% larger, because every 3 bytes become 4 characters.
Is Base64 a form of encryption?
No. Anyone can decode it without a key. Do not use it to hide sensitive documents.
What does = at the end of Base64 mean?
Padding. One = means the last group had 2 bytes; == means it had 1 byte.
Sources
- IETF RFC 4648 – The Base16, Base32, and Base64 Data Encodings
- IETF RFC 2397 – The "data" URL scheme
- MDN Web Docs – Base64 (glossary)
- MDN Web Docs – data: URLs
- MDN Web Docs – FileReader
Sources checked on 23 November 2026. Official notices and orders change; the latest version on the issuing body's website is final.