Base64 Encoder / Decoder
Encode or decode Base64 — Standard, URL-safe, or Raw (no padding). Code snippets for Go, Python, JS, Java, Rust.
What is Base64?
Base64 is an encoding scheme that converts binary data into a string of 64 printable ASCII characters. It was created to safely transmit binary content — like images, files, or binary payloads — through channels that only handle text, such as email (MIME), HTTP headers, or JSON fields.
The name "Base64" comes from the 64 characters it uses: uppercase A–Z, lowercase a–z, digits 0–9, and two special characters (+ and / in the standard variant). Every 3 bytes of binary input produce 4 Base64 characters, making the output about 33% larger than the original.
Base64 is not encryption — it is pure encoding. Anyone can decode it instantly. It exists only to make binary data safe to transport in text-only environments.
How Base64 Works
Base64 encoding works by grouping binary data into 6-bit chunks and mapping each chunk to one of 64 printable characters:
| Step | Example |
|---|---|
| Input text | Hi (2 bytes: 01001000 01101001) |
| Group into 3-byte blocks | 72, 105, pad |
| Split into 6-bit groups | 011001 000110 100100 padding |
| Map to Base64 alphabet | S G k = |
| Output | SGk= |
Base64 Variant Comparison
| Variant | Charset | Padding | Go | Use case |
|---|---|---|---|---|
| Standard | A-Z a-z 0-9 + / | = padding | base64.StdEncoding | Email (MIME), general data |
| URL-safe | A-Z a-z 0-9 - _ | = padding | base64.URLEncoding | JWT, URL params, cookies |
| Raw (URL-safe) | A-Z a-z 0-9 - _ | No padding | base64.RawURLEncoding | JWT tokens, compact storage |
Go Base64 Encodings
import "encoding/base64"
data := []byte("Hello, World!")
// Standard (+ / with padding)
std := base64.StdEncoding.EncodeToString(data)
// => "SGVsbG8sIFdvcmxkIQ=="
// URL-safe (- _ with padding)
url := base64.URLEncoding.EncodeToString(data)
// => "SGVsbG8sIFdvcmxkIQ=="
// Raw URL-safe (- _ NO padding) — used in JWT
raw := base64.RawURLEncoding.EncodeToString(data)
// => "SGVsbG8sIFdvcmxkIQ"
// Decode
decoded, err := base64.RawURLEncoding.DecodeString(raw)
fmt.Println(string(decoded)) // Hello, World!Common Use Cases
| Use case | Variant | Why |
|---|---|---|
| JWT (JSON Web Tokens) | Raw URL-safe | Compact, URL-safe, no = padding issues |
| HTTP Basic Auth | Standard | Authorization: Basic base64(user:pass) |
| Email attachments (MIME) | Standard | RFC 2045 specifies standard Base64 |
| URL query parameters | URL-safe | - and _ are URL-safe; + and / are not |
| Data URIs | Standard | data:image/png;base64,iVBOR... |
| Binary in JSON | Standard | JSON strings can contain Standard Base64 |