Developer Guide
What is Base64 Encoding? How It Works and When to Use It
Learn what Base64 encoding is, how the algorithm works, and when to use it for email attachments, APIs, and data URLs in web development.
Introduction
Understanding what Base64 encoding is and how it works is essential for any developer working with data transmission, APIs, or email systems. Base64 is an encoding scheme that converts binary data into a text-based ASCII format using 64 safe characters: A-Z, a-z, 0-9, plus (+), and forward slash (/), with equals signs (=) for padding. The purpose of Base64 is to represent binary data in a way that can be safely transmitted through systems that only handle text.
Base64 is NOT encryption. Anyone who sees a Base64-encoded string can easily decode it back to the original data. It is purely a format conversion tool, not a security tool. Never use Base64 for sensitive data that needs to stay secret—use proper encryption instead.
In this guide, you will learn what Base64 encoding is, how the algorithm works step by step, the Base64 alphabet table, common use cases like email attachments and data URLs, the difference between encoding and encryption, and how to decode Base64 strings.
How Base64 Encoding Works
Base64 works by taking binary data and converting it into a text representation using 64 characters. Each character represents 6 bits of binary data (since 2^6 = 64). This means every 3 bytes (24 bits) of original data become 4 Base64 characters (24 bits ÷ 6 bits per character = 4 characters).
The Base64 alphabet consists of: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
Let us encode the text "Hello" step by step:
Step 1: Convert to ASCII values: H=72, e=101, l=108, l=108, o=111
Step 2: Convert to binary: 01001000 01100101 01101100 01101100 01101111
Step 3: Group into 6-bit chunks: 010010 000110 010101 101100 011011 000110 111100
Step 4: Convert each 6-bit group to its Base64 character: SGVsbG8=
The equals signs (=) at the end are padding characters. Padding is added when the original data length is not a multiple of 3 bytes. One equals sign means one byte of padding, two equals signs mean two bytes of padding.
Base64 Alphabet Table
The Base64 alphabet maps 6-bit binary values to specific characters. Values 0-25 map to A-Z, 26-51 map to a-z, 52-61 map to 0-9, 62 maps to +, and 63 maps to /. For example, binary 000000 maps to A, 000001 maps to B, and so on. This table is standardized in RFC 4648 and is used consistently across all Base64 implementations.
Common Use Cases for Base64
Email Attachments: Email was designed to transmit text only. Before Base64, sending binary files like images and PDFs via email was impossible. Email systems (MIME) use Base64 to encode attachments before transmission. When you send a photo by email, it is Base64-encoded during transmission and decoded on the receiving end.
Data URLs in HTML: You can embed images directly in HTML using Base64 data URLs. Instead of linking to an external image file, you include the image data as a Base64 string: <img src="data:image/png;base64,iVBORw0KGgo..." />. This reduces HTTP requests but increases HTML size.
API Authentication: HTTP Basic Authentication requires encoding username:password in Base64. The Authorization header includes Basic followed by the Base64-encoded credentials: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==.
JSON API Responses: When APIs transmit binary data (like images or documents) as JSON, they often Base64-encode it since JSON only supports text. This is common in REST APIs that return image data encoded as Base64 strings.
JWT Tokens: JWT tokens use a URL-safe variant of Base64 for their header and payload components, replacing + with - and / with _ to avoid special characters in URLs.
Encoding vs Encryption
Base64 is encoding, not encryption. The key difference is that encoding is reversible using a well-known algorithm without any secret key. Anyone who receives a Base64 string can decode it using publicly available tools. Encryption, on the other hand, requires a secret key to decrypt the data. Encrypted data is unreadable without the key, while encoded data is just formatted differently.
Use Base64 when you need to convert binary data to a text format for transmission through text-only systems. Use encryption when you need to protect sensitive data from unauthorized access. Never use Base64 to hide sensitive information—it provides no security whatsoever.
How to Decode Base64
Decoding Base64 is the reverse of encoding. Each character is converted back to its 6-bit binary value, all the bits are concatenated, padding is removed, and the result is converted back to the original bytes. Most programming languages have built-in Base64 decoding functions. For example, in JavaScript you use atob() to decode and btoa() to encode. In Python, you use the base64 module.
Frequently Asked Questions
What is Base64 encoding used for?
Base64 encoding is used to transmit binary data through text-only systems like email, JSON APIs, and HTML. Common uses include encoding email attachments, embedding images in HTML as data URLs, encoding credentials for HTTP Basic Authentication, and encoding binary data in JSON responses.
Is Base64 secure?
No, Base64 is not secure. It is encoding, not encryption. Anyone can decode a Base64 string using freely available tools. Never use Base64 to protect sensitive data. Always use proper encryption algorithms like AES for security.
How do I decode a Base64 string?
Use a Base64 decoder tool or a built-in function in your programming language. Online tools let you paste Base64 strings and instantly decode them. In JavaScript, use atob() to decode. In Python, use base64.b64decode().
Does Base64 reduce file size?
No, Base64 actually increases data size by approximately 33%. Every 3 bytes of original data becomes 4 Base64 characters. This overhead is the trade-off for making binary data safe for text-only transmission.
What is the difference between Base64 and Base64URL?
Base64URL is a variant of Base64 that replaces + with - and / with _ and removes padding (=). This makes the encoded string safe for use in URLs and filenames without requiring percent-encoding. JWT tokens use Base64URL encoding.
Try Base64 Encoding Yourself
Now that you understand what Base64 encoding is, try encoding and decoding strings yourself. Use our free Base64 Encoder/Decoder online to instantly encode text to Base64 or decode Base64 strings—no signup required.
About the Author
Written by Zohaib Hassan, a web developer from Pakistan. Zohaib created Online Free Tools to help developers, students, and creators save time by providing quick access to essential utilities without installing software or creating accounts. When not coding, Zohaib writes technical guides to help others master web development concepts.
Published: June 28, 2026