Base64 速查表
Base64 用 64 个可打印的 ASCII 字符表示二进制数据,于是任何东西——文本、图片、密钥——都能通过只接受文本的通道传输。
核心事实
| 事实 | 说明 |
|---|---|
| Alphabet | A–Z, a–z, 0–9, + and / (64 characters) |
| Padding | = pads the output so its length is a multiple of 4 |
| Size cost | Encoded output is ~33% larger than the input bytes |
| URL-safe variant | Uses - and _ instead of + and / (JWTs use this, unpadded) |
| Decodable? | Always, by anyone — Base64 is encoding, NOT encryption |
| Typical uses | Data URLs, email attachments (MIME), JWT payloads, embedding fonts |
命令行一行搞定
| 任务 | 命令 |
|---|---|
| Encode text (macOS/Linux) | echo -n "hello" | base64 |
| Decode text | echo "aGVsbG8=" | base64 -d |
| Encode a file | base64 input.png > output.txt |
| Decode a file | base64 -d output.txt > input.png |
| URL-safe encode (Node.js) | Buffer.from(s).toString("base64url") |
常见问题
Is Base64 encryption?
No. Decoding requires no key — anyone can reverse it instantly. Never "protect" secrets by Base64-encoding them; that is obfuscation, and it fools nobody.
Why is the output bigger than the input?
Three bytes (24 bits) become four 6-bit groups, each written as a full character (8 bits): a 4/3 ratio, about +33%.
Why does my decoded string end in garbage?
Almost always trailing whitespace or a lost padding character. Make sure the = padding survived copying, and that URL-safe (-, _) variants are decoded with the right decoder.
别死记,直接用工具
Convert it locally in the Base64 Encoder