What is Base64 Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. Defined by RFC 4648, it uses 64 printable characters (A-Z, a-z, 0-9, +, and /) with = used for padding.
Base64 is widely utilized in web development for embedding images directly inside HTML and CSS (data URIs), transmitting authentication credentials (HTTP Basic Auth), sending email attachments (MIME), and encoding JWT tokens.
Base64 in JavaScript and Python
JavaScript / Node.js
// Browser:
const encoded = btoa("Hello World"); // "SGVsbG8gV29ybGQ="
const decoded = atob("SGVsbG8gV29ybGQ="); // "Hello World"
// Node.js:
const b64 = Buffer.from("Hello World").toString("base64");
const original = Buffer.from(b64, "base64").toString("utf8");Python 3
import base64
# Encode
encoded_bytes = base64.b64encode(b"Hello World")
print(encoded_bytes.decode("ascii")) # SGVsbG8gV29ybGQ=
# Decode
decoded_bytes = base64.b64decode("SGVsbG8gV29ybGQ=")
print(decoded_bytes.decode("utf-8")) # Hello World