Base64 for developers

Published

Every mainstream language ships a Base64 implementation, but they differ in the details that cause bugs: Unicode handling, URL-safe alphabets, padding, line breaks and strictness. These guides show the idiomatic way to do it in each language, and the traps to avoid.

Quick reference

Language Encode Decode
JavaScript (browser) btoa(binary) / Uint8Array.prototype.toBase64() atob(b64) / Uint8Array.fromBase64()
Node.js Buffer.from(data).toString('base64') Buffer.from(b64, 'base64')
Python base64.b64encode(data) base64.b64decode(b64)
PHP base64_encode($data) base64_decode($b64, true)
Java Base64.getEncoder().encodeToString(bytes) Base64.getDecoder().decode(b64)
C# Convert.ToBase64String(bytes) Convert.FromBase64String(b64)
Go base64.StdEncoding.EncodeToString(b) base64.StdEncoding.DecodeString(s)
Bash base64 -w0 file base64 -d file

Three rules that prevent most Base64 bugs

  1. Base64 encodes bytes, not text. Convert strings to bytes with an explicit character set (almost always UTF-8) before encoding, and back after decoding.
  2. Know your alphabet. URLs, JWTs and file names use Base64URL (- and _, usually no padding). Standard decoders reject it.
  3. Be strict when validating, lenient when reading. Reject malformed input from APIs, but strip whitespace and data URI prefixes when users paste values.

Guides by topic

Choose a language or topic below.

JavaScript How to encode and decode Base64 in JavaScript: btoa and atob, Unicode-safe UTF-8 conversion, Uint8Array.toBase64, Base64URL, files, images and Blob URLs. Python How to encode and decode Base64 in Python 3 with the base64 module: strings vs bytes, urlsafe_b64encode, validation, padding errors, files and images. PHP How to use base64_encode and base64_decode in PHP: strict decoding, URL-safe Base64, images as data URIs, files, chunk_split for e-mail and common pitfalls. Java Encode and decode Base64 in Java 8+ with java.util.Base64: basic, URL-safe and MIME encoders, without padding, streams, files and IllegalArgumentException. C# / .NET Encode and decode Base64 in C# and .NET: Convert.ToBase64String, FromBase64String, TryFromBase64String, Base64Url in .NET 9, UTF-8 strings, files and streams. Go Encode and decode Base64 in Go with encoding/base64: StdEncoding, URLEncoding, RawURLEncoding, streaming encoders, strict mode and common decode errors. Node.js Encode and decode Base64 in Node.js using Buffer: strings, files, images, Base64URL, streaming large files and validating input. Copy-ready examples. Bash / Linux Encode and decode Base64 on the Linux, macOS and Windows command line: the base64 command, -w0, openssl base64, certutil, files, strings and newline pitfalls. Data URI Data URIs (data URLs) explained: syntax, Base64 vs URL encoding, MIME types, browser limits, performance, security, and examples for HTML and CSS. CSS Base64 data URIs in CSS HTML Base64 data URIs in HTML

Comments (0)

Questions, ideas or a bug in “Base64 for developers”? Leave a comment, rate the tool, or reply to others.

No comments yet. Be the first to share your feedback!

Leave a comment

Rate this tool:
Esc
↑ ↓ move↵ openEsc close