Base64 编码与解码工具

解码 Base64

关于 Base64

什么是 Base64 编码?

RFC 4648 Base64 把每三个字节转换为四个 ASCII 字符,字母表为 A-Z、a-z、0-9、+ 和 /。末尾不足一组时用 = 填充,因此任意字节都能通过纯文本格式传输。

为什么要使用 Base64 编码?

Base64 常用于在 MIME 附件、JSON 字段、证书或 Data URL 中承载二进制字节。它会增加约 33% 的体积,不提供压缩、加密或完整性校验;本解码器要求标准字母表和有效填充。

Bash 中的 Base64

# Encoding
echo -n "Hello, World!" | base64
# Output: SGVsbG8sIFdvcmxkIQ==

# Decoding
echo "SGVsbG8sIFdvcmxkIQ==" | base64 -d
# Output: Hello, World!
			

JavaScript 中的 Base64

// Encoding
let encoded = btoa("Hello, World!");
console.log(encoded);
// Output: SGVsbG8sIFdvcmxkIQ==

// Decoding
let decoded = atob("SGVsbG8sIFdvcmxkIQ==");
console.log(decoded);
// Output: Hello, World!

			

Go 语言中的 Base64

package main
import (
	"encoding/base64"
	"fmt"
)
func main() {
	// Encoding
	encoded := base64.StdEncoding.EncodeToString([]byte("Hello, World!"))
	fmt.Println(encoded)
	// Output: SGVsbG8sIFdvcmxkIQ==

	// Decoding
	decoded, _ := base64.StdEncoding.DecodeString("SGVsbG8sIFdvcmxkIQ==")
	fmt.Println(string(decoded))
	// Output: Hello, World!
}
			

PHP 中的 Base64

// Encoding
$encoded = base64_encode("Hello, World!");
echo $encoded ."\n";
// Output: SGVsbG8sIFdvcmxkIQ==

// Decoding
$decoded = base64_decode("SGVsbG8sIFdvcmxkIQ==");
echo $decoded . "\n";
// Output: Hello, World!