Repairing a copied Base64 value with missing padding
A copied Base64 value can lose its final equals signs. Before adding them back, find out which variant the receiving field expects, because the standard and URL-safe decoders handle padding differently.
What the equals signs do
Base64 output is built from groups of four characters. When the input length is not a multiple of three bytes, the last group is short, and the encoder adds one or two equals signs to make four. One leftover byte gives two characters plus two equals signs, as in Zg==. Two leftover bytes give three characters plus one equals sign, as in Zm8=. Three bytes fill a group exactly, so no padding appears. Padding carries no data. It tells the decoder how many bytes the last group holds, and it makes the length a clean multiple of four, which simple decoders rely on.
Why copying drops them
Equals signs are punctuation to many programs. Chat applications, web forms and word processors may trim them at the end of a selection or treat them as markup. A value in a URL query string can lose them too, because the equals sign separates names from values, and some frameworks strip the trailing run. The result is a string whose length is one or two characters short of a multiple of four. The first case here decodes the intact Zg== and returns f, which shows the full padded form working.
The standard tool rejects Zg
The second case removes the equals signs and tries to decode Zg. The standard decoder on this site rejects it with a message about valid padding. That reflects RFC 4648, where padding is required unless the using specification says it can be omitted, and the tool applies the strict reading. The decoder could have guessed that two characters need two equals signs, but accepting every shortened input would blur the line between a complete value and one that lost characters in transit. Rejecting it makes you decide. Some libraries do add missing padding automatically, and others, including the decoders in several web browsers, accept it either way. Code that works against one decoder can therefore fail against another, so test with the decoder that will do the real work. Where a specification is silent, writing the full padded form is the choice that every decoder accepts.
Decide before you repair
Adding the equals signs back is safe only when the text was shortened by padding loss. If something else was dropped, the repaired string will decode to wrong data without an error. Check the length: a value that is one character short of a multiple of four needs one equals sign, two short needs two, and exactly one more than a multiple of four cannot be valid Base64 at all, because one leftover character carries only six bits. Look at the source system as well. If it documents unpadded output, the value was never meant to have the signs.
Standard versus URL-safe Base64
The URL-safe variant swaps plus and slash for minus and underscore, and it is frequently written without padding, as in JSON Web Tokens, where the specification omits it. The URL-safe tool on this site accepts an unpadded value. If your value contains minus or underscore characters, it is probably URL-safe and does not need repair, only the right decoder. If it contains plus or slash, it follows the standard alphabet, and the strict padding policy applies. Using the wrong tool is a more common reason for a failed decode than damaged padding. A token pulled from a web address, a cookie or an authorization header is usually in the URL-safe form, while a value taken from an email attachment, a data URI or an older configuration file is usually standard. Look at the characters before you choose a tool, and look at the documentation of the system that produced the value if the characters do not settle it.
A complete group needs nothing
The third case decodes Zm9v and returns foo. These four characters are one complete group of three bytes, so there are no equals signs and none are missing. Appending one would produce an invalid string. This is a frequent mistake when people apply the rule add padding until the length is a multiple of four blindly, then forget that a value already at a multiple of four is finished. The note on the case says exactly that: do not append padding merely because a value has none.
Counting characters to find the missing signs
Counting is the quickest diagnosis. Remove any whitespace you are sure was added, then divide the number of remaining characters by four. A remainder of zero means the value is complete. A remainder of two means two equals signs are missing, and a remainder of three means one is missing. A remainder of one cannot come from valid Base64, since a single leftover character holds only six bits, less than a byte, so the value lost more than padding. Zg has two characters and a remainder of two, and the complete form is Zg==. A value like Zm8 has three characters and a remainder of three, and the complete form is Zm8=. Run these two through the decoder to see the difference.
Keep a copy and test small
Work on a duplicate of the original value. Try the three controls first so you know what accepted, rejected and complete-group inputs look like in this tool. Then apply what you learned: count the characters, choose the variant, add equals signs only if the arithmetic calls for it and decode again. A successful decode shows the string fits this decoder. It cannot prove that no character went missing elsewhere in the copy, so compare the decoded text with what you expected. If you cannot recognize the output as text, stop and ask for a fresh copy instead of adjusting the string further, since every extra edit makes it harder to find where the loss occurred.
Limits of the text decoder
These tools decode to UTF-8 text. A correctly padded value that holds binary data, such as an image or a key, can be rejected because the bytes are not valid text, which is a separate problem from padding. The related guide on binary bytes in Base64 covers that. Padding repair addresses only the length rule, and it never recovers characters that went missing from the middle of the value. Equally, adding padding to a value that was cut off mid-group produces a string that can decode to the wrong text without an error, so the length arithmetic above is a necessary check and not a sufficient one.
Reproduce the checks
One text byte needs padding
Open the Base64 Encoder Decoder and enter:
- Calculation mode
decode- Text or encoded input
Zg==
Expected output:
Text
fThe two equals signs complete this encoded group.
The standard tool rejects the shortened form
Open the Base64 Encoder Decoder and enter:
- Calculation mode
decode- Text or encoded input
Zg
Expected rejection:
valid paddingThis rejection is the tool’s strict standard-Base64 policy.
A complete three-byte group needs no padding
Open the Base64 Encoder Decoder and enter:
- Calculation mode
decode- Text or encoded input
Zm9v
Expected output:
Text
fooDo not append padding merely because a value has none.
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
- Primary definition and convention reference ↗ · 2026-10-08