Base64 Line Wrapping (76 Characters)

Learn about the 76-character line length limit in MIME Base64. Understand why it exists, when to use it, and how different implementations handle line wrapping.

Format

Detailed Explanation

In MIME-formatted Base64 (RFC 2045), the encoded output is wrapped into lines of no more than 76 characters, with each line terminated by a carriage return and line feed (CRLF, \r\n). This is a critical formatting rule for email and certain legacy protocols, but is unnecessary for modern web usage.

Why 76 characters?

The SMTP protocol (RFC 5321) specifies a maximum line length of 998 characters (excluding CRLF). The original SMTP RFC (821, from 1982) recommended 78 characters. The MIME designers chose 76 characters to leave room for the CRLF line terminator and stay well within the conservative recommendation, ensuring compatibility with even the oldest and most restrictive mail servers.

Wrapped vs. unwrapped example:

Unwrapped (modern web usage):

SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBsb25nZXIgc3RyaW5nIHRoYXQgd2lsbCBwcm9kdWNlIG1vcmUgdGhhbiA3NiBjaGFyYWN0ZXJzIG9mIEJhc2U2NCBvdXRwdXQu

Wrapped (MIME format):

SGVsbG8gV29ybGQhIFRoaXMgaXMgYSBsb25nZXIgc3RyaW5nIHRoYXQgd2ls
bCBwcm9kdWNlIG1vcmUgdGhhbiA3NiBjaGFyYWN0ZXJzIG9mIEJhc2U2NCBv
dXRwdXQu

How implementations handle wrapping:

Python:

import base64
# No wrapping (standard)
base64.b64encode(data)
# With MIME wrapping (76 chars + \n)
base64.encodebytes(data)

Java:

// No wrapping
Base64.getEncoder().encodeToString(data);
// With MIME wrapping (76 chars + \r\n)
Base64.getMimeEncoder().encodeToString(data);
// Custom line length
Base64.getMimeEncoder(64, "\r\n".getBytes()).encodeToString(data);

OpenSSL (command line):

# Default: wraps at 76 characters
openssl base64 -in file.bin
# No wrapping
openssl base64 -A -in file.bin

When to use wrapped Base64:

  • Constructing MIME email messages.
  • PEM-formatted certificates and keys (which use 64-character lines, not 76).
  • Legacy systems that impose line length limits.

When to use unwrapped Base64:

  • Web APIs (JSON payloads, HTTP headers).
  • Data URIs in HTML/CSS.
  • Database storage.
  • Any modern application.

Decoding wrapped Base64: Most decoders silently ignore whitespace (including line breaks) in Base64 input. However, some strict decoders do not. If you receive MIME-formatted Base64, stripping all whitespace before decoding is the safest approach:

const cleaned = mimeBase64.replace(/\s/g, "");
const decoded = atob(cleaned);

Use Case

Generating PEM-formatted SSL certificates where the Base64-encoded certificate body must be wrapped at 64 characters per line between BEGIN and END markers.

Try It — Base64 Encoder

Open full tool