TEXT ENCODING

Checking line breaks in a pasted Base64 value

A newline inside an encoded string is different from a newline in the original text. The strict decoder rejects the first, and the second becomes part of what gets encoded.

Two places a line break can hide

A pasted Base64 value can contain a line break for two unrelated reasons. The encoded text may have been wrapped, by an email client, a terminal or a mail-style encoder, so the break sits inside the Base64 characters. Or the original message may have ended with a newline before it was encoded, in which case the break is represented by encoded characters and does not show up as a break at all. The first case damages the encoded string. The second is ordinary data. Telling them apart before you edit anything saves you from deleting a character that belonged to the message.

Why the strict decoder rejects Zm then 9v

The first case feeds the decoder the value Zm, a line break, then 9v. Read without the break, those characters are Zm9v, which decodes to foo. With the break present, the decoder meets a character that is not in the Base64 alphabet. The standard defined in RFC 4648 says implementations should reject input outside the alphabet unless a specification says otherwise, and this tool follows the strict reading. The tool reports a problem with valid padding, which is its general message for a value whose shape does not fit the strict format. Read the message as a clue that the length or layout is wrong, not as proof that the final characters are missing. A line break is a single character in the input, but a person scanning the screen often sees only two short lines of text and assumes the value is whole. Reading the characters one at a time, or pasting the value into an editor that marks line ends, makes the extra character visible.

The wrapped formats that allow breaks

Line breaks inside Base64 are normal in some formats, which is why people expect decoders to tolerate them. MIME, used in email attachments, wraps encoded data at 76 characters per line. PEM files, such as certificates and keys, wrap at 64 characters between BEGIN and END lines. Those standards define the wrapping, and their decoders skip the breaks on purpose. A strict RFC 4648 decoder, as used here, is not one of those. If your value came from a PEM block or a MIME body, the wrapping belongs to that container, and you can remove the breaks yourself before pasting the continuous text.

Encoding the word foo

The second case encodes the three letters foo with no newline and returns Zm9v. Three bytes make exactly one four-character group, so there is no padding. This is the control for the rejected value: it shows what the clean version of the broken example looks like. If you remove the line break from the first case by hand, you get this string. The lesson holds for longer values too: when a decode fails with a padding message, first compare your string to what an encoder produces for text you expect, and check whether anything was inserted between the characters.

Encoding foo with a trailing newline

The third case encodes foo followed by a line feed, which is four bytes, and returns Zm9vCg==. The extra characters Cg== represent the newline byte, 0A. This is how a newline in the source appears: it is a few encoded characters at the end, and nothing in the output is visibly broken. Many command-line habits produce this accidentally. Running echo foo and encoding the result includes the newline that echo adds, while printf without a newline does not. The two outputs, Zm9v and Zm9vCg==, look nearly alike and decode to different messages. The byte count explains the padding: foo is three bytes and fills one group, while the newline adds a fourth byte that starts a new group, which is then padded with two equals signs. A comparison of hashes or encoded strings between two systems will fail if only one of them added the newline, so record in your notes whether the source text included a final line break.

Deciding which break you have

Paste the encoded value into a plain text editor that shows whitespace. A break inside the encoded string will appear as a line ending in the middle of the run of letters and digits. A break in the decoded message shows only after decoding. If decoding succeeds and the result ends with an extra line, the source had a trailing newline, and whether to keep it depends on your use. Do not strip a newline from the decoded text unless you know the original did not carry one, because doing so changes the message you are trying to reproduce. A practical test is to re-encode the decoded text and compare it with the pasted value: if the strings match except for the final line, the newline came from the message.

Cleaning a wrapped value safely

When the wrapping is the problem, remove only the line feeds and carriage returns between the encoded characters, then try the decode again. Keep the original copy untouched, and do not remove trailing equals signs or reorder anything. A value that decodes after the breaks are gone confirms that wrapping was the only issue. A value that still fails points to another cause, such as a missing character, a stray space, or a URL-safe alphabet with minus and underscore signs instead of plus and slash. Those belong to the separate Base64url tool. If you must clean many values, do it in a script that removes only the characters 0A and 0D, so that a stray space or tab remains visible as an error instead of being silently discarded. Silent cleanup makes it harder to notice that a source system is damaging its output.

Carriage returns and Windows line endings

Line endings differ between systems. Unix-style systems end a line with a line feed, byte 0A. Windows text files end it with a carriage return and a line feed, bytes 0D 0A. Classic Mac files used a carriage return alone. If the pasted encoded value came from a Windows file, each break in it is two characters, and both are outside the Base64 alphabet. The same holds for the decoded message: text typed on Windows and encoded with its line ending contains two extra bytes, which encode to the characters DQo= when they stand alone. Knowing the line ending that produced a value explains why two encodings of apparently identical text, made on different machines, do not match.

Reproducing the result

Each of the three cases can be repeated in the Base64 tool: decode Zm, a line break and 9v to see the rejection, encode foo to get Zm9v, and encode foo followed by a newline to get Zm9vCg==. In a one-line input field you may not be able to type a line break, so use a multi-line input or paste one in. If the tool shows the rejection for a value you consider clean, check for a hidden carriage return left by copying across operating systems, which looks the same as a line feed and breaks decoding in the same way.

Reproduce the checks

An inserted break is rejected

Open the Base64 Encoder Decoder and enter:

Calculation mode
decode
Text or encoded input
Zm
9v

Expected rejection:

valid padding

The encoded text contains a newline between groups of characters.

Encode letters alone

Open the Base64 Encoder Decoder and enter:

Calculation mode
encode
Text or encoded input
foo

Expected output:

Text

Zm9v

There is no newline in the source.

Encode a trailing newline

Open the Base64 Encoder Decoder and enter:

Calculation mode
encode
Text or encoded input
foo

Expected output:

Text

Zm9vCg==

The source newline is represented inside the encoded data.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks