Base64 encoding explained
How Base64 turns bytes into text, why it adds a third to the size, how padding and URL-safe variants work and why it is not encryption.
By the tools4free.dev team · Last reviewed 4 December 2026
Base64 is a way of writing arbitrary bytes using only 64 safe text characters: the letters A-Z and a-z, the digits 0-9, and + and /, with = used as padding. It exists so binary data, such as an image or a signature, can travel through systems built for text, like email, JSON and HTTP headers. It is an encoding, not encryption: anyone can reverse it, and it adds no security.
How the encoding works
Base64 takes the input three bytes (24 bits) at a time and splits those 24 bits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, which selects a character from the alphabet: 0-25 are A-Z, 26-51 are a-z, 52-61 are 0-9, 62 is + and 63 is /.
Worked example 1: encoding "Man" by hand
The text Man is three bytes: 0x4D, 0x61 and 0x6E. In binary that is 01001101 01100001 01101110. Regroup the same 24 bits into sixes:
| 6-bit group | Value | Character |
|---|---|---|
010011 | 19 | T |
010110 | 22 | W |
000101 | 5 | F |
101110 | 46 | u |
So Man encodes to TWFu. Three bytes became four characters, which is why Base64 output is about one third larger than the input: 100 bytes become 136 characters.
Padding
When the input length is not a multiple of three, the last group is short. Base64 fills it with zero bits and adds = characters so the output length is a multiple of four.
| Input | Bytes | Output | Padding |
|---|---|---|---|
ABC | 3 | QUJD | none |
AB | 2 | QUI= | one = |
A | 1 | QQ== | two = |
Hello, World! | 13 | SGVsbG8sIFdvcmxkIQ== | two = |
Padding can never be more than two characters. Some URL and token formats omit the padding because the length already tells the decoder how many bytes the last group holds.
Base64 and Unicode
Base64 encodes bytes, not characters. Text must first become bytes in some encoding, and UTF-8 is the standard choice. This catches out JavaScript developers, because the built-in btoa() function accepts only characters in the range U+0000 to U+00FF and throws an error for anything else:
btoa('hello'); // "aGVsbG8="
btoa('€'); // throws InvalidCharacterError
// Correct: encode to UTF-8 bytes first
const bytes = new TextEncoder().encode('€ café');
const bin = String.fromCharCode(...bytes);
btoa(bin); // "4oKsIGNhZsOp"
// Decode back
const back = new TextDecoder().decode(
Uint8Array.from(atob('4oKsIGNhZsOp'), c => c.charCodeAt(0))
);
// "€ café"
In Node.js it is shorter: Buffer.from('€ café').toString('base64') gives the same 4oKsIGNhZsOp, and Buffer.from(str, 'base64').toString('utf8') reverses it. Decoding Base64 as text only works if you know the original bytes were text in the encoding you assume. Random bytes, such as an encrypted blob, do not decode into readable text.
Base64URL: the URL-safe variant
The characters + and / have special meanings in URLs and file names, and = is used in query strings. RFC 4648 therefore defines a second alphabet that replaces + with - and / with _, and usually drops the padding. For example, the three bytes 0xFB 0xFF 0xFE encode to +//+ in standard Base64 and to -__- in Base64URL. JWTs, many API keys and URL-safe identifiers use this form. Mixing the two variants is a common reason a decoder reports an invalid character.
Worked example 2: HTTP Basic authentication
HTTP Basic authentication sends username:password as Base64 in a header. The example from RFC 7617 uses the user Aladdin and the password open sesame:
"Aladdin:open sesame" -> QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
This is the clearest demonstration that Base64 is not protection. Anybody who sees the header can decode it in one step. Basic authentication is acceptable only over HTTPS, where the transport itself is encrypted.
Calculating the encoded size
For an input of n bytes, the padded Base64 length is 4 × ceil(n / 3) characters. A 1 MB file of 1,048,576 bytes therefore becomes 1,398,104 characters, about 1.33 MB. Without padding the length is ceil(n × 4 / 3). This matters when you embed images as data URIs or send files in JSON bodies that have a size limit: a limit of 1 MB on the request means the file itself can be only about 750 KB.
Worked example 3: decoding by hand
Decoding reverses the steps. Take SGk=. The characters S, G and k have values 18, 6 and 36, which in 6-bit binary are 010010, 000110 and 100100. Joined, that is 010010000110100100 followed by the padding, which stands for no data. Regroup into bytes and drop the incomplete trailing bits: 01001000 is 0x48 (H) and 01101001 is 0x69 (i). The result is Hi. One = told the decoder the final group carried only two bytes of real data.
Where you will meet Base64
- Data URIs:
data:image/png;base64,...embeds a small file in HTML or CSS. It avoids a request but makes the document about a third larger and cannot be cached separately. - Email (MIME): attachments are Base64-encoded, with lines wrapped at 76 characters under RFC 2045.
- JSON and APIs: JSON has no binary type, so binary fields such as file contents or cryptographic keys travel as Base64 strings.
- PEM files: the text between
-----BEGIN CERTIFICATE-----and-----END CERTIFICATE-----is Base64 of a binary structure. - Tokens: each part of a JWT is Base64URL without padding.
Common mistakes
- Calling it encryption. Base64 hides nothing. Never use it to "protect" passwords, API keys or personal data.
- Forgetting UTF-8. Encoding non-ASCII text with
btoadirectly either fails or produces different output from other languages. Always convert to UTF-8 bytes first. - Using the wrong alphabet. Feeding standard Base64 into a Base64URL decoder, or the reverse, fails on
+,/,-and_. Convert, or choose the matching decoder. - Stripping or adding padding carelessly. Some decoders require the
=signs and others reject them. Re-add padding until the length is a multiple of four before using a strict decoder. - Encoding large files into JSON. The one-third size increase and the memory needed to hold both copies get expensive. Upload files as binary instead.
- Leaving whitespace in the value. Line breaks from MIME or copied text break strict decoders. Remove them first.
Frequently asked questions
Why does Base64 make data bigger?
It uses 6 bits of information per 8-bit character, so the output is 4/3 the size of the input, plus a little padding.
Is Base64 the same as hexadecimal?
No. Hex uses 16 symbols and doubles the size. Base64 uses 64 symbols and grows by a third, so it is more compact.
Can I tell if a string is Base64?
Only roughly. It uses a restricted alphabet and its length is a multiple of four when padded, but many ordinary words also fit that pattern, such as "test" or "abcd". Context matters more than appearance.
Is Base64 safe for secrets in config files?
It is safe only in the sense that it avoids special characters. Kubernetes Secrets, for example, store values in Base64, but that is formatting rather than encryption. Control access to the file instead.
Try it: encode and decode text yourself with the encoder and decoder. It runs in your browser, so the text is not sent anywhere.