What Is Base64 Encoding and How Does It Work?
Base64 turns any data into plain letters and digits so it can travel safely through systems built for text. Learn how the encoding works bit by bit, where you meet it every day and why it is not encryption.
If you have ever opened the raw source of an email, looked inside a web token or seen an image written as a long string starting with data:image/png;base64,, you have seen Base64. It looks like random gibberish, but it follows a simple, exact recipe. Base64 exists to solve one problem: moving arbitrary bytes, such as an image or a file, through channels that were designed to carry only plain text.
The problem Base64 solves
Computers store everything as bytes, values from 0 to 255. Many of those values are not printable characters, and some have special meanings in older systems: a zero byte can end a string, certain values are treated as line breaks, and some mail servers once stripped the top bit off every byte. Sending a photo through such a system could quietly corrupt it.
Base64 sidesteps all of that by using only 64 characters that every text system handles safely:
- the uppercase letters A–Z (values 0–25)
- the lowercase letters a–z (values 26–51)
- the digits 0–9 (values 52–61)
- the plus sign + (62) and the slash / (63)
Plus the equals sign = for padding at the end.
How the encoding works, bit by bit
Sixty-four values need exactly 6 bits, because 2^6 = 64. A byte has 8 bits. The least common multiple of 6 and 8 is 24, so Base64 works on groups of 3 bytes (24 bits) and turns each group into 4 characters (4 × 6 bits).
Let's encode the text Hi! by hand.
- Convert each character to its byte value: H = 72, i = 105, ! = 33.
- Write the bytes in binary: 01001000 01101001 00100001.
- Split the 24 bits into four 6-bit groups: 010010 000110 100100 100001.
- Convert each group to a number: 18, 6, 36, 33.
- Look up each number in the alphabet: 18 = S, 6 = G, 36 = k, 33 = h.
The result is SGkh. Decoding simply runs the same steps backward.
What the = signs mean
Not every input divides neatly into groups of three bytes. When the last group is short, the encoder pads it with zero bits and marks the missing bytes with = signs:
Hi!is 3 bytes and encodes toSGkhwith no padding.Hiis 2 bytes and encodes toSGk=with one =.His 1 byte and encodes toSA==with two.
So a Base64 string always ends with zero, one or two = signs, never three. A longer example: Hello, world! is 13 bytes, which is four full groups plus one leftover byte, so it becomes 20 characters ending in ==: SGVsbG8sIHdvcmxkIQ==.
Why Base64 output is bigger
Every 3 bytes become 4 characters, so the output is about 33% larger than the input. The exact length is 4 × (number of bytes ÷ 3, rounded up). A 1,000,000-byte file becomes 1,333,336 characters of Base64. That overhead is the price of safety, and it is why Base64 is best for small pieces of data rather than large files.
Text, UTF-8 and emoji
Base64 encodes bytes, not letters. Before text can be encoded it has to become bytes, and the modern standard for that is UTF-8. Plain English letters take one byte each, but accented letters, other alphabets and emoji take two to four. The text naïve ☕ is only 7 characters but 10 bytes in UTF-8, so it encodes to bmHDr3ZlIOKYlQ==. Some older tools assume one byte per character and fail on text like this; a tool that converts to UTF-8 first gets it right in both directions.
Where you meet Base64
- Email attachments. The MIME standard encodes files in Base64 so they survive mail servers built for text.
- Data URLs. Small images, icons or fonts can be embedded directly in HTML or CSS as
data:image/png;base64,..., saving a separate download. - Web tokens. JSON Web Tokens are three URL-safe Base64 sections separated by dots. Anyone can decode the first two and read their contents.
- HTTP Basic authentication. The browser sends the username and password joined with a colon and encoded in Base64, which is why that method must only be used over HTTPS.
- APIs and config files. Binary data like certificates or small files is often stored as Base64 inside JSON, XML or YAML.
The URL-safe variant
Two of the standard characters cause trouble in web addresses: + can be read as a space and / separates path segments. The URL-safe variant replaces them with - and _, and usually drops the = padding. Hello, world! in URL-safe form is SGVsbG8sIHdvcmxkIQ. A good decoder accepts both forms.
How to use the Base64 Encoder & Decoder
Choose Encode or Decode and type or paste your text; the Base64 Encoder & Decoder updates the result as you type and handles UTF-8 properly. Tick URL-safe output when the result is going into a link or a file name. Decoding accepts standard and URL-safe input, with or without padding, and ignores line breaks. You can also turn a file of up to 10 MB into plain Base64 or a ready-to-paste data URL. Everything runs in your browser, so your text and files stay on your device.
Base64 is not encryption
This is the most important point. Base64 has no key and no secret: anyone can decode it instantly. Encoding a password or an API key in Base64 hides it from nobody. If data must stay private, it needs real encryption, and if a password must be stored, it needs a proper password hash. Treat any Base64 string you find as readable plain text.
FAQ
How can I tell if a string is Base64?
Look for a string made only of A–Z, a–z, 0–9, + and / (or - and _), with a length that is a multiple of four when padded, possibly ending in one or two = signs. That is a strong hint, but not proof: short ordinary words like Test are valid Base64 too.
Can Base64 be used to compress data?
No. It always makes data larger, by about a third. If you need smaller data, compress it first with a format like gzip or zip and then Base64-encode the compressed bytes if it has to travel as text.
Why does my decoded output look like garbled symbols?
The Base64 is probably valid, but the bytes inside are not text. They may be an image, a PDF or compressed data. Decode it to a file instead of to text, or check what format the sender used.
Is Base64 the same everywhere?
The core alphabet is defined in the RFC 4648 standard, and the standard and URL-safe variants are the ones you will meet almost everywhere. Older systems sometimes insert line breaks every 76 characters, which decoders normally ignore.