Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

RFC 3548 defines common alphabets and rules for Base16, Base32, and Base64 encoding to improve interoperability and security. It is a historical specification superseded by RFC 4648.

This article explains the specification, with additional context and examples.

My other articles cover EIPs, BIPs, SLIPs, CAIPs, ENSIPs, and RFCs.

Overview

Base encodings such as Base64 represent binary data as text. Some older systems and restricted environments only accept US-ASCII, making it necessary to represent arbitrary bytes using a defined text alphabet.

These encodings remain useful in modern systems because encoded data can be transferred through text-oriented protocols and handled with text editors.

Historical problems

Applications and protocols historically used slightly different variants of base encoding. Even implementations called Base64 could differ in:

  • Line wrapping.
  • Handling characters outside the encoding alphabet.

Objectives

RFC 3548 defines common alphabets and encoding rules to reduce ambiguity between specifications and implementations and improve interoperability.

Implementation

Earlier implementations differed in line breaks, padding, and invalid character handling. RFC 3548 establishes common default rules: do not insert line breaks, include required padding, and reject characters outside the alphabet unless a referring specification explicitly says otherwise.

Line breaks

MIME limits encoded Base64 lines to 76 characters. Earlier PEM conventions used 64-character lines. These conventions were influenced by restrictions of historical SMTP systems.

Base64 itself does not require line breaks. Encoders must not add them unless the specification using the encoding explicitly requires them.

Padding

When Base64 input length is not divisible by three, the encoder uses trailing equals signs as padding. Padding provides a consistent representation of the final partial group. Some applications explicitly permit omitted padding; otherwise, required padding must be included.

Characters outside the alphabet

Unexpected characters can arise from corrupted data, invalid input, or deliberate manipulation. Accepting them can create security problems, including weaknesses in unsafe decoder implementations.

Decoders must reject characters outside the alphabet unless a referring specification explicitly permits ignoring them. MIME, for example, permits a more liberal approach, including ignoring line breaks. A referring specification may also allow certain excess trailing padding characters to be ignored.

Choosing an alphabet

Different applications have different requirements. Humans can confuse 0 with O, or 1 with lowercase l and uppercase I. Base32 avoids the digits 0 and 1. In URLs and filenames, slash characters may be interpreted as path separators, while search tools may treat plus and slash as word boundaries.

No single alphabet is ideal for every context. RFC 3548 defines several alphabets for different use cases.

Specification

How Base64 works

Base64 represents arbitrary sequences of octets using ASCII characters. It is not intended to make binary content human-readable, but makes it easier to transmit through systems designed for text.

It uses 64 alphabet characters to represent six-bit values, plus the equals sign for padding.

Encoding steps

  1. Divide the input into three-byte (24-bit) groups.
  2. Split each group into four six-bit values.
  3. Interpret each value as an integer from 0 to 63.
  4. Look up the corresponding Base64 character.

Three input bytes become four output characters.

Base64 alphabet

The alphabet maps uppercase letters to indices 0–25, lowercase letters to 26–51, digits to 52–61, plus to 62, and slash to 63. The equals sign is used for padding.

text
A–Z → 0〜25
a–z → 26〜51
0–9 → 52〜61
+   → 62
/   → 63
=   → パディング用

Padding

When the final input group contains fewer than three bytes, the encoder fills the unused bits with zeros and adds the required padding.

  1. A complete 24-bit group produces four characters without padding.
  2. A final eight-bit group produces two alphabet characters followed by two equals signs.
  3. A final 16-bit group produces three alphabet characters followed by one equals sign.

With padding, the output length is a multiple of four characters.

Example

Encode the string "Man". The ASCII values are M = 77, a = 97, and n = 110.

  • Their binary representations are:

    text
    M   = 01001101
    a   = 01100001
    n   = 01101110
  • Splitting the combined 24 bits into six-bit groups gives:

    text
    010011 010110 000101 101110
  • Mapping these groups to alphabet indices produces:

    text
    19 = T, 22 = W, 5 = F, 46 = u

The output is "TWFu".

URL- and filename-safe Base64

Standard Base64 uses plus and slash characters. Slash can be interpreted as a path separator and plus may represent a space in some query-string conventions.

The URL-safe variant uses the same underlying encoding procedure but replaces those two alphabet characters.

Differences from standard Base64

Three input bytes still produce four characters, and the padding rules remain the same unless the enclosing protocol specifies otherwise.

  • Standard Base64: index 62 is plus and index 63 is slash.
  • URL-safe Base64: index 62 is hyphen and index 63 is underscore.

Alphabet

text
A–Z → 0〜25
a–z → 26〜51
0–9 → 52〜61
-   → 62
_   → 63
=   → パディング用

The ranges are unchanged; only the characters for indices 62 and 63 differ.

Usage

URL-safe Base64 is commonly used in web applications and APIs. It should be distinguished from standard Base64, for example by calling it base64url or URL-safe Base64. Padding may still need special handling in a URL, depending on the enclosing protocol.

Example

Compare standard and URL-safe Base64 for the byte string "??".

python
import base64

data = b"??"

# 通常のBase64
print(base64.b64encode(data))  
# 出力: b"Pz8="

# URL-safe Base64
print(base64.urlsafe_b64encode(data))  
# 出力: b"Pz8="

Both outputs are Pz8= in this example. When a standard encoded value contains plus or slash, the corresponding URL-safe characters are hyphen and underscore. The Japanese comments in the original code label standard Base64 and the output.

How Base32 works

Base32 represents binary data with 32 alphabet characters: uppercase A–Z and digits 2–7. It does not use separate uppercase and lowercase alphabet symbols. Whether a decoder accepts lowercase input depends on the applicable specification and implementation.

Alphabet

text
A–Z → 0〜25
2–7 → 26〜31
=   → パディング用

The alphabet has 26 letters and six digits. Digits zero and one are omitted to reduce visual ambiguity. The equals sign is reserved for padding.

Encoding steps

  1. Divide the input into five-byte (40-bit) groups.
  2. Split each group into eight five-bit values.
  3. Interpret each value as an integer from 0 to 31.
  4. Replace it with the corresponding alphabet character.

Five input bytes produce eight characters. Bits are processed from most significant to least significant within each input octet.

Padding

If the final group has fewer than five bytes, unused bits are filled with zeros and padding is appended. The padded output length is a multiple of eight.

  1. Five remaining bytes produce eight characters with no padding.
  2. One byte produces two characters and six equals signs.
  3. Two bytes produce four characters and four equals signs.
  4. Three bytes produce five characters and three equals signs.
  5. Four bytes produce seven characters and one equals sign.

Example

Encode the string "f". Its ASCII value is 102, or 01100110 in binary.

  • The following illustrates the input extended to a 40-bit group for explanation:

    text
    01100110 00000000 00000000 00000000 00000000
  • Divide into five-bit groups:

    text
    01100 11000 00000 00000 00000 00000 00000 00000

The displayed indices are 12, 24, and zeros. Only the first two alphabet characters represent the original byte; the remaining positions are padding. The output is "MY======".

How Base16 works

Base16, commonly called hexadecimal or hex, represents arbitrary binary data using 16 symbols. Each character represents four bits. One byte always becomes two characters, doubling the length of the encoded representation.

Alphabet

text
0 1 2 3 4 5 6 7 8 9 A B C D E F

The symbols represent values from zero to fifteen: A represents ten, and F represents fifteen. The specified alphabet uses uppercase characters; lowercase hexadecimal is also commonly accepted by compatible implementations.

Encoding steps

  1. Read one eight-bit byte.
  2. Split it into a high nibble and a low nibble, each containing four bits.
  3. Interpret each nibble as a value from 0 to 15.
  4. Write the two corresponding hexadecimal characters.

Repeat for each input byte.

Why padding is unnecessary

Base32 and Base64 may need padding when the final input group is incomplete. Base16 always represents a byte using exactly two characters, so no equals-sign padding is needed.

Example

Encode "Hi". The ASCII value for H is 72 (hex 48), and i is 105 (hex 69).

text
4869

Two input bytes become four hexadecimal characters.

Worked examples

Visualizing Base64

Base64 transforms three octets into four six-bit indices. Each index selects an alphabet character.

text
+--first octet--+-second octet--+--third octet--+
|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
+-----------+---+-------+-------+---+-----------+
|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
+--1.index--+--2.index--+--3.index--+--4.index--+

The diagram illustrates the relationship between the three input bytes and four output indices.

Visualizing Base32

Base32 transforms five octets into eight five-bit indices.

text
        1          2          3
01234567 89012345 67890123 45678901 23456789
+--------+--------+--------+--------+--------+
|< 1 >< 2| >< 3 ><|.4 >< 5.|>< 6 ><.|7 >< 8 >|
+--------+--------+--------+--------+--------+

The first character represents the first five bits, the second the next five bits, and so on.

Base64 worked examples

The following examples use hexadecimal inputs of different lengths.

Example 1: Input 0x14fb9c03d97e

text
入力データ (Hex):  14 fb 9c 03 d9 7e
2進数(8ビット単位): 00010100 11111011 10011100 00000011 11011001 11111110
6ビットごとに区切り: 000101 001111 101110 011100 000000 111101 100111 111110
10進数:               5      15     46     28     0      61     37     62
出力(Base64):         F      P      u      c      A      9      l      +

The result is "FPucA9l+". The original diagram contains a transcription error: the final byte 0x7e is binary 01111110, not 11111110, and the corresponding penultimate six-bit group is 100101, not 100111. The numeric index 37 and output l in the example are correct.

Example 2: Input 0x14fb9c03d9

text
入力データ (Hex):  14 fb 9c 03 d9
2進数:             00010100 11111011 10011100 00000011 11011001
6ビット分割:       000101 001111 101110 011100 000000 111101 100100
10進数:             5      15     46     28     0      61     36
出力(Base64):       F      P      u      c      A      9      k
パディング:         =

The result is "FPucA9k=", with one padding character.

Example 3: Input 0x14fb9c03

text
入力データ (Hex):  14 fb 9c 03
2進数:             00010100 11111011 10011100 00000011
6ビット分割:       000101 001111 101110 011100 000000 110000
10進数:             5      15     46     28     0      48
出力(Base64):       F      P      u      c      A      w
パディング:         =      =

The result is "FPucAw==", with two padding characters. The diagrams retain the original labels for hexadecimal input, binary groups, decimal indices, output, and padding.

Security considerations

Implementation risks

Base16, Base32, and Base64 implementations must avoid vulnerabilities such as buffer overflows. Decoders should safely reject malformed inputs. An unexpected null character must not cause a crash, out-of-bounds access, or unintended truncation.

Handling invalid characters

Rejecting characters outside the specified alphabet is the safe default. A permissive decoder that ignores unexpected characters may create a covert channel, in which additional information is carried by characters that do not affect the decoded bytes.

Likewise, case-insensitive representations can permit information to be conveyed through differences in letter case. Implementers should understand these potential risks and follow the applicable canonicalization and validation rules.

Encoding provides no cryptographic protection

Base encoding changes representation, not confidentiality. A Base64-encoded password can be decoded trivially.

Sensitive information has been exposed when users share encoded network data without realizing it contains credentials. Treat encoded secrets as secrets, and use appropriate cryptographic protection when confidentiality is required.

Conclusion

RFC 3548 defines a common approach to Base16, Base32, and Base64 encoding. Its rules cover alphabets, line breaks, padding, and invalid characters to improve interoperability and security.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

You can also find my work on the following sites.

https://chaldene.net/