Base64 编码 / 解码器

这个 Base64 工具可将文本编码为 Base64 格式,并将 Base64 字符串解码回可读文本。Base64 是一种二进制到文本的编码方案,使用 64 个 ASCII 字符表示二进制数据,常用于电子邮件、数据 URL 和 API 身份验证。

Base64 Encoder / Decoder

Encode text to Base64 or decode Base64 back to text

常见问题

什么是Base64编码?

Base64编码是一种将二进制数据转换为64个ASCII字符(A-Z、a-z、0-9、+和/)字符串的二进制到文本编码方案。它允许二进制数据通过基于文本的协议安全传输,例如嵌入HTML/CSS中作为data URI,或包含在JSON和XML文档中,而不会损坏数据。

Base64编码是如何工作的?

Base64的工作原理是将每3个字节(24位)分成4组,每组6位。每个6位组映射到Base64字母表中的64个字符之一。如果输入长度不能被3整除,则添加填充字符(=)。例如,文本"Hi"(2字节)产生"SGk=",其中包含一个填充字符。

为什么使用Base64?

当二进制数据需要以文本形式表示时就会使用Base64。常见用途包括:在HTML/CSS data URL中嵌入图像、对邮件附件(MIME)进行编码、在JSON或XML中存储二进制数据、HTTP基本身份验证头,以及以PEM格式对加密密钥和证书进行编码。

Base64和加密有什么区别?

Base64是一种编码方案,不是加密。它将数据转换为不同的表示形式,但不提供任何安全性。任何人都可以在没有密钥的情况下解码Base64。相比之下,加密使用算法和密钥使数据在没有正确密钥的情况下无法读取。Base64用于数据传输兼容性,而不是保密性。

Base64比原始数据大多少?

Base64编码使数据大小增加约33%。每3字节输入产生4字节Base64输出。例如,3KB的文件经Base64编码后约为4KB。这种开销是以文本格式安全表示二进制数据所需的代价。

什么是URL安全Base64?

URL安全Base64将标准的+和/字符分别替换为-和_,通常省略=填充。此变体用于URL、文件名和其他标准Base64字符会造成问题的场景。它通常用于JWT令牌、URL参数和文件系统安全标识符。

Base64可以编码二进制文件吗?

是的,Base64可以编码任何二进制文件,包括图像、PDF、音频、视频和可执行文件。文件以原始字节方式读取,然后编码为Base64文本。这通常用于将小图像直接嵌入HTML或CSS、将文件附加到邮件,以及通过纯文本API传输二进制数据。

如何在JavaScript、Python或命令行中进行Base64编码?

在JavaScript中,使用btoa()编码,atob()解码:btoa("Hello")返回"SGVsbG8="。在Python中,使用base64模块:base64.b64encode(b"Hello").decode()返回"SGVsbG8="。在命令行中,使用:echo -n "Hello" | base64(macOS/Linux)。解码分别使用atob()、base64.b64decode()或echo "SGVsbG8=" | base64 --decode。

什么是Base64 data URI?

data URI使用Base64编码将文件内容直接嵌入HTML或CSS中。格式为data:[mediatype][;base64],<数据>。例如,小PNG图像可以嵌入为<img src="data:image/png;base64,iVBOR..."/>。Data URI消除了额外的HTTP请求,但会增加约33%的HTML大小,因此最适合5-10KB以下的小文件。

Base64和Base32有什么区别?

Base64使用64个字符(A-Z、a-z、0-9、+、/),输出紧凑,大小增加约33%。Base32只使用32个字符(A-Z、2-7),大小增加约60%,但输出不区分大小写,避免了像0/O和1/l这样容易混淆的字符。Base32用于TOTP/2FA验证码,而Base64是一般数据编码的首选。

About Base64 Encoder / Decoder

The Base64 Encoder / Decoder is a versatile online tool that lets you encode text into Base64 format or decode Base64 strings back into readable text. Base64 encoding is widely used in web development, email systems, and data transmission to safely represent binary data as ASCII text. This Base64 Encoder / Decoder handles Unicode characters seamlessly, making it suitable for multilingual content.

What is Base64 Encoding?

Base64 is a binary-to-text encoding scheme that represents binary data using 64 ASCII characters (A-Z, a-z, 0-9, +, /). It converts every 3 bytes of input into 4 Base64 characters, with padding (=) added when the input length is not a multiple of 3. This Base64 Encoder / Decoder uses the standard Base64 alphabet defined in RFC 4648.

How to Use the Base64 Encoder / Decoder

  1. Select the "Encode" tab to convert plain text to Base64, or the "Decode" tab to convert Base64 back to text
  2. Enter or paste your text into the input area
  3. The Base64 Encoder / Decoder instantly converts your input in real time
  4. Click the copy button to copy the result to your clipboard
  5. Review the size comparison to understand the encoding overhead

Common Use Cases for Base64 Encoding

  • Embedding images directly in HTML or CSS using data URIs
  • Encoding email attachments in MIME format
  • Transmitting binary data through text-only protocols like JSON or XML
  • Storing binary data in databases that only support text
  • Encoding authentication credentials in HTTP Basic Auth headers
  • Passing binary data in URL query parameters

How Base64 Encoding Works

The Base64 encoding process converts input data through these steps:

  1. The input text is converted to its binary byte representation (UTF-8)
  2. The binary data is split into groups of 6 bits each
  3. Each 6-bit group is mapped to one of the 64 Base64 characters
  4. Padding characters (=) are added if needed to make the output length a multiple of 4

Base64 encoding increases the data size by approximately 33%, since 3 bytes of input produce 4 bytes of output. The Base64 Encoder / Decoder displays this size comparison for every conversion.

Base64 Character Set

The standard Base64 alphabet (RFC 4648) maps each 6-bit value (0-63) to a specific character:

IndexCharIndexCharIndexCharIndexChar
0A16Q32g48w
1B17R33h49x
2C18S34i50y
3D19T35j51z
4E20U36k520
5F21V37l531
6G22W38m542
7H23X39n553
8I24Y40o564
9J25Z41p575
10K26a42q586
11L27b43r597
12M28c44s608
13N29d45t619
14O30e46u62+
15P31f47v63/

URL-safe Base64 replaces + (index 62) with - and / (index 63) with _.

Base64 in Programming Languages

Common Base64 encoding and decoding patterns across popular languages:

LanguageEncodeDecode
JavaScriptbtoa(str)atob(str)
Pythonbase64.b64encode(b)base64.b64decode(s)
JavaBase64.getEncoder().encode(b)Base64.getDecoder().decode(s)
C#Convert.ToBase64String(b)Convert.FromBase64String(s)
PHPbase64_encode($s)base64_decode($s)
Gobase64.StdEncoding.EncodeToString(b)base64.StdEncoding.DecodeString(s)
CLI (macOS/Linux)echo -n str | base64echo str | base64 --decode

Unicode and UTF-8 Support

This Base64 Encoder / Decoder fully supports Unicode characters by first encoding text as UTF-8 bytes before applying the Base64 transformation. This means you can safely encode and decode text containing special characters, emojis, and characters from any language. The tool uses the TextEncoder and TextDecoder APIs to ensure correct handling of multibyte characters.

Tips for Working with Base64

  • Base64 is an encoding scheme, not encryption — it provides no security
  • Base64 encoded strings are always longer than the original data (~33% increase)
  • When using Base64 in URLs, consider URL-safe Base64 which replaces + with - and / with _
  • Remove any whitespace or line breaks from Base64 strings before decoding
  • Valid Base64 strings have a length that is a multiple of 4 (with padding)

相关工具