0Pricing
Cryptology Academy · Lesson

Base64 Encoding: How It Works

Decode the mystery of Base64 — the encoding scheme used in emails, JWTs, and data transmission.

Base64 Encoding: How It Works is a free Cryptology Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cryptology Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Binary Needs Text Representation

Many systems, including email, HTTP headers, and XML, were designed to handle text but not arbitrary binary data. A byte value of 0x00 (null) or 0x1A (control character) can cause parsing failures or data corruption.

Base64 encoding converts arbitrary binary data into a subset of ASCII characters that is safe for text-only systems, allowing binary content like images and certificates to be embedded in text-based protocols.

6-Bit Grouping from 8-Bit Bytes

Base64 works by taking three 8-bit bytes (24 bits total) and splitting them into four 6-bit groups. Each 6-bit group represents a value from 0 to 63, which maps to one character in the Base64 alphabet.

This 3-to-4 ratio means every 3 bytes of input produce 4 characters of output, a 33% size increase. The math works out evenly because 3 bytes = 24 bits = 4 groups of 6 bits.

The Base64 Alphabet

The Base64 alphabet has 64 characters: uppercase A-Z (indices 0-25), lowercase a-z (indices 26-51), digits 0-9 (indices 52-61), plus + (index 62), and forward slash / (index 63).

This alphabet avoids all control characters and most punctuation that could cause problems in text systems. Every character in the Base64 output is a printable ASCII character safe for use in virtually any text protocol.

Padding with = Signs

When the input length is not a multiple of 3 bytes, padding is needed. If one byte remains, it is padded to a 12-bit group producing 2 Base64 characters followed by "==". If two bytes remain, they produce 3 Base64 characters followed by "=".

The = padding signs are not encoded data; they signal the decoder that the last block is shorter than 3 bytes. They ensure the output length is always a multiple of 4 characters.

Encoding Example: Man

The ASCII values for "Man" are M=77 (01001101), a=97 (01100001), n=110 (01101110). Concatenated: 010011010110000101101110.

Split into 6-bit groups: 010011 | 010110 | 000101 | 101110 = 19 | 22 | 5 | 46. Looking up the Base64 alphabet: 19=T, 22=W, 5=F, 46=u. "Man" encodes to "TWFu".

Base64url Variant

Standard Base64 uses + and / which have special meanings in URLs and filenames. Base64url is a variant that replaces + with - and / with _, making it safe for use in URLs, filenames, and cookie values.

JWTs (JSON Web Tokens) use Base64url encoding for their header and payload sections. JOSE (JSON Object Signing and Encryption) standards are built entirely on Base64url.

Where Base64 Is Used

Base64 is used in email MIME attachments (encoding binary files for text-based email), JWT tokens (encoding JSON header and payload), data URIs in HTML (embedding images directly in CSS/HTML), and X.509 certificate PEM format.

PEM (Privacy Enhanced Mail) format wraps DER-encoded binary certificates in Base64 between "-----BEGIN CERTIFICATE-----" and "-----END CERTIFICATE-----" headers.

Base64 Provides No Security

Base64 is not encryption. It is a reversible encoding with no key. Anyone who sees Base64-encoded data can decode it immediately using freely available tools or a one-liner in any programming language.

A common mistake is thinking that Base64-encoded passwords or tokens are "hidden". They are not. If you can decode it, so can an attacker. Security requires encryption, not encoding.

Decoding Is Trivial

Decoding Base64 is just as easy as encoding. Each 4-character Base64 group is mapped back to its 6-bit values, concatenated, and split into bytes. The = padding indicates how many bytes the last group represents.

In Python: import base64; base64.b64decode("TWFu") returns b"Man". The process is deterministic, lossless, and requires no key or secret.

Base64 Overhead and Efficiency

The 33% size overhead of Base64 (4 output bytes per 3 input bytes) plus newlines every 76 characters (as required by MIME) means Base64 is not efficient for large binary transfers.

Modern binary protocols like HTTP/2 with binary framing and WebSockets with binary frames can handle raw bytes directly, eliminating the need for Base64 in high-performance contexts.

Base32 and Base58: Other Encoding Schemes

Base32 uses a 32-character alphabet (A-Z and 2-7), producing output that is longer than Base64 but case-insensitive and suitable for use in contexts where case may be lost. TOTP shared secrets are often encoded in Base32.

Base58, used by Bitcoin for wallet addresses, removes visually ambiguous characters like 0, O, I, and l. This reduces transcription errors when copying addresses. The tradeoff is that Base58 is not a power of 2, making encoding slightly more complex.

Base64 Alphabet Quiz

Test your knowledge of Base64 encoding.

Key Takeaways: Base64 Encoding

Base64 converts binary data to text by grouping bits into 6-bit chunks and mapping them to a 64-character alphabet. It produces a 33% size overhead and uses = padding for non-multiples of 3 bytes.

Base64 provides no security. It is used for transmission compatibility, not confidentiality. Base64url (-/_) is the URL-safe variant used in JWTs. Decoding requires no key and takes milliseconds.

Frequently asked questions

Is the “Base64 Encoding: How It Works” lesson free?

Yes — the full text of “Base64 Encoding: How It Works” is free to read here on the web, and the Cryptology Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cryptology Academy course, upgrade to CoddyKit PRO.

What will I learn in “Base64 Encoding: How It Works”?

Decode the mystery of Base64 — the encoding scheme used in emails, JWTs, and data transmission. You practise Cryptology Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Cryptology Academy?

No prior experience is required. Cryptology Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Base64 Encoding: How It Works” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Cryptology Academy lesson?

Yes. Every Cryptology Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Base64 Encoding: How It Works
  2. ASCII, Unicode, and Text Representation
  3. Hexadecimal in Cryptographic Output
  4. Encoding vs Encryption vs Hashing
← Back to Cryptology Academy