What is base64 encoding?
Base64 turns any data, like an image, into ordinary letters and numbers so it can travel through systems that only handle plain text. Images and other files are stored as binary data, the raw 1s and 0s a computer works with. Most of that data doesn’t line up with letters you can type. Encoding is the step that turns that binary into text and decoding turns the text back into the exact original data.
The name comes from the 64 characters it uses, which are A to Z, a to z, 0 to 9, + and /. Computers count binary in bytes, which are groups of eight 1s and 0s. Base64 reads the data 3 bytes at a time and writes each group as 4 characters, which makes the encoded result about a third bigger than the original. The data doesn’t always split evenly into groups of 3, so base64 adds one or two = signs at the end as padding to fill out the last group.
You can try it in a terminal:
echo -n "hello" | base64
# aGVsbG8=
echo "aGVsbG8=" | base64 --decode
# hello
JavaScript has the same thing built in with the btoa function to encode and atob to decode.
I see base64 all the time, mostly in JWTs but also in API payloads. Each of the three parts of a JWT is encoded with base64url, a variant that swaps + and / for - and _ so the result is safe to put in a URL. APIs use it to send a file inside JSON, since JSON has no way to hold raw binary data. It also shows up in data URIs that embed a small image straight into HTML or CSS, in email attachments and in the HTTP Basic Auth header, where your username and password get joined with a colon and encoded.
People often mix up base64 with encryption but they work very differently. Anyone can decode base64 with one command and no key, so it hides nothing. That means anyone holding a JWT can read what’s inside it. Basic Auth credentials sent without HTTPS are basically plain text to anyone watching the connection. Encryption scrambles data so it can only be read by someone with the right key. Base64 is also different from a hash, which can’t be turned back into the original at all.