Base64
Embed This Widget
Add the script tag and a data attribute to embed this widget.
Embed via iframe for maximum compatibility.
<iframe src="https://unicodefyi.com/iframe/glossary/base64/" width="420" height="400" frameborder="0" style="border:0;border-radius:10px;max-width:100%" loading="lazy"></iframe>
Paste this URL in WordPress, Medium, or any oEmbed-compatible platform.
https://unicodefyi.com/glossary/base64/
Add a dynamic SVG badge to your README or docs.
[](https://unicodefyi.com/glossary/base64/)
Use the native HTML custom element.
Binary-to-text encoding that represents binary data using 64 ASCII characters (A–Z, a–z, 0–9, +, /). Used for embedding binary data in text-based protocols like email (MIME) and data URIs.
What is Base64?
Base64 is a binary-to-text encoding scheme that represents arbitrary binary data using a set of 64 printable ASCII characters. The name comes directly from the size of the character set. It is defined in RFC 4648 (The Base16, Base32, and Base64 Data Encodings) and is one of the most widely deployed encoding schemes in computing, appearing in email attachments, data URIs, HTTP Basic Authentication, JSON Web Tokens, and cryptographic certificate formats.
The need for Base64 arises because many communication protocols and storage systems were designed for text and cannot reliably transmit arbitrary binary bytes. Email protocols (SMTP), HTTP headers, and early internet protocols treated certain byte values as control characters with special meaning. Base64 sidesteps this problem entirely by encoding every byte as one or two printable characters.
The Base64 Alphabet
The standard Base64 alphabet (RFC 4648 Table 1) consists of:
- Uppercase letters A–Z (26 characters)
- Lowercase letters a–z (26 characters)
- Digits 0–9 (10 characters)
- Plus sign
+and forward slash/(2 characters)
Together these 64 characters can each represent exactly 6 bits of data (2⁶ = 64). Base64 encodes input in 3-byte (24-bit) groups, converting each group into four 6-bit values and looking up each value in the alphabet. Three input bytes always produce exactly four output characters — a 33% size increase.
Padding: If the input length is not a multiple of 3, the output is padded with one or two = characters to make the output length a multiple of 4.
import base64
# Encoding
data = b"Hello, Unicode!"
encoded = base64.b64encode(data)
# b'SGVsbG8sIFVuaWNvZGUh'
# Decoding
decoded = base64.b64decode(encoded)
# b'Hello, Unicode!'
URL-Safe Variant (Base64url)
The standard + and / characters are significant in URLs, making standard Base64 unsafe for use in query strings, filenames, or JWT tokens. RFC 4648 defines Base64url, which replaces + with - and / with _. Padding = characters are often omitted in Base64url contexts.
# URL-safe encoding (used in JWTs, data URIs for some contexts)
encoded_url = base64.urlsafe_b64encode(data)
Common Use Cases
Data URIs: Browsers support inline resources encoded as data: URIs. A small PNG icon can be embedded directly in HTML or CSS without a network request:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==
MIME email (RFC 2045): Email bodies and attachments are Base64-encoded to survive transmission through SMTP servers that may modify bytes with values above 127 or interpret CR/LF sequences.
HTTP Basic Authentication: Credentials are Base64-encoded (though not encrypted) in the Authorization header: Authorization: Basic dXNlcjpwYXNzd29yZA==
JSON Web Tokens (JWT): Each section of a JWT (header, payload, signature) is Base64url-encoded and joined with dots.
Quick Facts
| Property | Value |
|---|---|
| Defined in | RFC 4648 |
| Character set size | 64 printable ASCII characters |
| Encoding ratio | 3 bytes input → 4 characters output (33% overhead) |
| Padding character | = (one or two) |
| URL-safe variant | Base64url: replaces +→-, /→_ |
| Key use cases | MIME email, data URIs, JWT, HTTP Basic Auth |
| Python module | import base64 (stdlib) |
相关术语
编码 中的更多内容
美国信息交换标准代码。7位编码,涵盖128个字符(0–127),包括控制字符、数字、拉丁字母和基本符号。
Visual art created from text characters, originally limited to the 95 printable …
主要在台湾和香港使用的繁体中文字符编码,收录约13,000个CJK字符。
扩展二进制编码十进制交换码。IBM大型机编码,字母范围不连续,至今仍用于银行和企业大型机。
基于KS X 1001的韩语字符编码,将韩文音节和汉字映射为双字节序列。
简体中文字符编码系列:GB2312(6,763字)经GBK演化为GB18030,成为与Unicode兼容的中国强制性国家标准。
由IANA维护的字符编码名称官方注册表,用于HTTP Content-Type头和MIME(如charset=utf-8)。
针对不同语言组的8位单字节编码系列,ISO 8859-1(Latin-1)是Unicode前256个码位的基础。
将单字节ASCII/JIS罗马字与双字节JIS X 0208汉字相结合的日语字符编码,仍在传统日语系统中使用。
仅覆盖BMP(U+0000–U+FFFF)的废弃固定2字节编码,是UTF-16的前身,无法表示补充字符。