Base64 Encode and Decode Explained (With Simple Examples)

Sooner or later you run into a long string like SGVsbG8= in an email header, an API response or a web page's source, and wonder what it is. That is Base64. This guide explains what Base64 encoding is used for, how it turns bytes into text, how to Base64 decode a string in your browser, and the mistakes that trip people up, using short examples you can try yourself.
What Base64 actually is
Base64 is a way of writing any data, including images and other binary files, using only 64 safe characters: A–Z, a–z, 0–9, + and /, with = used as padding at the end.
Why bother? Many systems were built to carry plain text and can damage raw bytes: email, URLs, JSON fields, HTML attributes, config files. Base64 converts bytes into characters those systems will not mangle, and the receiver converts them back.
How it works, in one example
Take the word Man. In bytes it is three characters, 24 bits. Base64 cuts those 24 bits into four groups of 6 bits. Each 6-bit number (0 to 63) is looked up in the 64-character alphabet:
| Input | Base64 |
|---|---|
Man |
TWFu |
Hi |
SGk= |
Hello |
SGVsbG8= |
café |
Y2Fmw6k= |
Two things follow from this:
- Every 3 bytes become 4 characters, so Base64 output is about a third bigger than the original. A 300 KB image becomes roughly 400 KB of text.
- The
=at the end is padding. When the input is not a multiple of 3 bytes, one or two=signs fill the last group.Hihas 2 bytes, so it gets one=.
Non-English text works too, as long as it is first turned into UTF-8 bytes. That is why café gives Y2Fmw6k=: the é takes two bytes.
What Base64 is used for
- Embedding images in HTML, CSS or emails as data URLs, such as
data:image/png;base64,.... Small icons can live inside the page instead of being separate files. - Email attachments. Mail was designed for text, so attachments are Base64 encoded behind the scenes.
- APIs and JSON. When an API needs to send a file inside a JSON field, Base64 is the usual choice.
- Basic HTTP authentication, where
user:passis sent asdXNlcjpwYXNz. - Tokens such as JWTs, which use a URL-safe variant called Base64url.
Base64 is not encryption
This is the most important point in the whole post. Anyone can decode Base64 in a second; there is no key and no secret. If you see a password or API key "protected" with Base64 in a config file, treat it as plain text. To protect data you need real encryption. To protect a document, use something like Protect PDF, which sets an actual password.
How to encode or decode Base64 on DoraPDF
- Open the Base64 Encoder / Decoder.
- Choose the Mode: Encode → Base64 or Decode ← Base64.
- Paste your text into the input box. The result appears live in the output box.
- Click Copy to grab it.
To encode a file, scroll to "Or encode a file" and drop any file on the drop area. You get a complete data URL (with the file type at the front, for example data:image/png;base64,) that you can paste straight into an <img src=""> or a CSS url().
Text is handled as UTF-8 in both directions, so accented letters, Urdu, Arabic and emoji survive the round trip.
Base64 vs URL encoding
People often mix these up because both make text "safe". They solve different problems:
- Base64 turns any data into a compact set of characters.
- URL encoding (percent encoding) only escapes the characters that have a special meaning in a web address.
price=10€ & taxbecomesprice%3D10%E2%82%AC%20%26%20tax.
If you are building a link with a search term or a redirect address inside it, you want the URL Encoder / Decoder, not Base64. Standard Base64 output can itself contain +, / and =, which are special in URLs, so a Base64 string placed in a URL usually needs URL encoding too.
Base64url and JWTs
A JSON Web Token looks like three Base64 chunks joined by dots: header.payload.signature. It uses Base64url, where + becomes -, / becomes _, and the padding is usually dropped.
The first chunk of a typical token, eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9, decodes to {"alg":"HS256","typ":"JWT"}. To read a whole token, paste it into the JWT Decoder. It decodes the header and payload, shows them as formatted JSON, and if there is an exp claim it shows the expiry date and whether the token has already expired. Note that it does not verify the signature, so it tells you what a token says, not whether it is genuine.
Common problems and fixes
- "Invalid character" when decoding. The string probably contains
-or_, meaning it is Base64url. Replace-with+and_with/, or use the JWT Decoder if it is a token. Also check for stray spaces or line breaks in the middle. - Decoded text is garbage. The original was probably binary, like an image or a ZIP file, not text. Decoding it as text will not show anything readable.
- Missing padding. Some systems strip the
=signs. If decoding fails, try adding=until the length is a multiple of 4. - Data URL does not display. Make sure you copied the whole string, including the
data:...;base64,prefix, and that nothing cut it off.
Privacy
The Base64 tool, URL Encoder and JWT Decoder all work in your browser. Text, files and tokens are processed on your device and are not uploaded. That matters with JWTs in particular: a live token is often enough to act as the logged-in user, so avoid pasting it into tools that send it to a server.
FAQ
Is Base64 secure? No. It is an encoding, not encryption. Anyone can reverse it without a key.
Why does Base64 end with = or ==? That is padding, added when the input length is not a multiple of 3 bytes.
Why is the Base64 version bigger than my file? Every 3 bytes become 4 characters, so output is about 33% larger.
Can I decode an image from Base64 here? The decoder is for text. You can turn an image into a data URL with the file encoder, and a browser can display that data URL directly.
Try the tools from this guide — free
Keep reading
Free Developer Tools Online: JSON Formatter, Regex Tester, Base64, URL Encoder, JWT Decoder, SQL and UUID
Eight small browser-based developer tools for everyday debugging: validate JSON, test regex, decode JWTs and Base64, format SQL, make UUIDs. Free.
GuideBest Free PDF Website for Students? Why Students Choose DoraPDF
What students need from a free PDF website (no sign-up, no watermark, works on phone, private) and how DoraPDF covers the full assignment chain.
GuideIs Paid PDF Software Worth It? Free vs Paid PDF Tools, Task by Task
Free vs paid PDF software: which everyday tasks free DoraPDF tools cover, when paying makes sense, and a 5-minute test before you renew.