Base64 in real life: everyday examples
Base64 is one of those things you use every day without noticing. It is inside your email, inside the web pages you read, and inside the logins your apps send to their servers. This guide shows where it appears, what it is doing in each place, and why the people who built those systems chose it, with real numbers for each example. Try any of the encodings on the Base64 Encoder Decoder as you read.
Email attachments
Email was designed for text, and it still only carries text reliably. When you attach a photo or a document, your email app turns the file into Base64 text before sending it, and the receiver's app turns it back into the file. A 100-kilobyte photo becomes about 133,000 characters of text inside the message, which is why large attachments make emails slow to send and why most email services limit attachment sizes. The encoding is invisible to you, but you can see it if you ever look at the raw source of an email: long lines of letters and digits, wrapped at 76 characters per line, with the attachment hidden inside them. Every attachment you have ever sent travelled this way.
Images inside web pages
A web page can carry a small image inside itself instead of loading it as a separate file. The image is encoded as Base64 and written into the page after the words data:image/png;base64, which is a 21-character label telling the browser what follows. The browser reads the label, decodes the text, and shows the picture. This is handy for tiny images such as icons, because it saves a separate download, but it costs a third more characters, so a 3-kilobyte icon becomes 4 kilobytes of text inside the page. Large pictures are never embedded this way; the size cost is too high, and a separate file is cheaper. If you have ever seen a web address starting with data:image, you have seen Base64 in a web page.
Logins sent to servers
When an app sends your username and password to a server, it often joins them with a colon and encodes the result. The pair user:pass is nine bytes, and it encodes to dXNlcjpwYXNz, which is 12 characters. The server decodes it, splits it at the colon, and checks the two halves. This is the basic authentication scheme used by countless APIs, and it is important to be clear about what it does and does not do: the encoding only makes the login safe to carry in a header, and it hides nothing. Anyone who intercepts dXNlcjpwYXNz can decode it to user:pass in a second, which is why it must always travel over an encrypted connection. Base64 is the envelope, not the lock.
Data saved in files and databases
Files and databases that store text often need to hold binary data as well, and Base64 is the usual bridge. A settings file written in JSON cannot hold raw bytes, because JSON is text, so a small file or a token is stored as a Base64 string instead. A database column that holds text can store an image the same way. The cost is the same as everywhere else: about a third more space, so a 1-megabyte file stored this way takes about 1,398,104 characters of storage instead of 1,048,576 bytes. That is why large files are stored as files, not as text, while small pieces of binary data — tokens, keys, thumbnails — live happily as Base64 strings inside otherwise plain-text records.
Why it keeps showing up
Base64 keeps appearing for the same reason in every case: the channel only accepts text, and the data is not text. Email, web pages, headers and JSON files are all text, and the things people need to send through them — pictures, files, passwords — are not. Base64 is the simplest bridge between the two, it is built into every programming language, and its output is safe to copy, paste, print and store. It is not the fastest bridge and it is not the smallest, but it is the one everyone agrees on, and that agreement is worth more than a cleverer design. Whenever you see a long string of letters ending in one or two equals signs, you are looking at that bridge doing its job.
Frequently asked questions
Where is Base64 used in real life?
In email attachments, in images embedded in web pages, in logins sent to servers, and in binary data stored inside text files and databases.
Why are email attachments sent as Base64?
Email only carries text reliably, so a file is encoded into Base64 text before sending and decoded back into the file on arrival.
What is data:image/png;base64, in a web address?
A label telling the browser that a Base64-encoded image follows, so the picture can travel inside the page instead of as a separate file.
Is the Base64 login in an API header secure?
No. Encoding user:pass as dXNlcjpwYXNz hides nothing, since anyone can decode it. It must always travel over an encrypted connection.
More guides on the Base64 Encoder Decoder
Try the Base64 Encoder Decoder — free, in your browser.