Skip to main content

Fixed: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

I passed one part of a JSON Web Token (JWT) to PowerShell's [Convert]::FromBase64String, and it refused with The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters. The part was base64url, which differs from base64 in two ways, and fixing only one of them isn't enough.

What I Was Doing​

I was writing How to Decode Base64url Text in the Terminal, in PowerShell 7.6. I had the payload part of a small test token, {"sub":"zoe","team":"qa?"} once decoded, and gave it straight to .NET's base64 decoder:

PowerShell
$part = 'eyJzdWIiOiJ6b2UiLCJ0ZWFtIjoicWE_In0'
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($part))

What I Expected​

The JSON inside:

Expected output
{"sub":"zoe","team":"qa?"}

What I Got​

Output
MethodInvocationException: Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

What I Checked​

What FromBase64String accepts. Its documentation lists the characters: "The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", lowercase characters "a" to "z", numerals "0" to "9", and the symbols "+" and "/"." It throws a FormatException when "The length of s, ignoring white-space characters, is not zero or a multiple of 4", or when "s contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters." PowerShell reports that exception as a MethodInvocationException.

The part itself. It contains a _, which isn't on that list, and it's 35 characters long:

PowerShell
$part.Length
Output
35

35 isn't a multiple of 4, so the part also breaks the length rule.

Each problem on its own. Adding the missing = but keeping the _ still fails, and swapping the _ but leaving out the = still fails, with the same message:

PowerShell
[Convert]::FromBase64String($part + '=').Length
[Convert]::FromBase64String($part.Replace('-', '+').Replace('_', '/')).Length
Output
MethodInvocationException: Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."
MethodInvocationException: Exception calling "FromBase64String" with "1" argument(s): "The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters."

A PowerShell 7.6 terminal where FromBase64String rejects a JWT part with the error The input is not a valid Base-64 string, the part is 35 characters long, and both adding an = and swapping the characters on their own still give the same error.

Why It Happens​

JWTs store their parts in base64url, not base64. RFC 4648 defines base64url with - and _ in place of + and /, and RFC 7515, the JWT signing standard, also drops the = padding: its parts use base64url "with all trailing '=' characters omitted". FromBase64String only reads standard base64, so a JWT part breaks both of its rules at once: a - or _ is "a non-base 64 character", and the missing padding makes the length wrong.

The Fix​

Swap the two characters back, then pad with = up to the next multiple of 4:

PowerShell
$base64 = $part.Replace('-', '+').Replace('_', '/')
$base64 = $base64.PadRight($base64.Length + (4 - $base64.Length % 4) % 4, '=')
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($base64))

(4 - $base64.Length % 4) % 4 works out how many = are missing: here one, because 35 leaves a remainder of 3.

PowerShell 7.5 and later run on .NET 9 or newer, which has a decoder made for base64url. It accepts the part as it is:

PowerShell
[Text.Encoding]::UTF8.GetString([System.Buffers.Text.Base64Url]::DecodeFromChars($part))

After the Fix​

Both ways printed the JSON:

Output
{"sub":"zoe","team":"qa?"}
{"sub":"zoe","team":"qa?"}

The same PowerShell terminal after the fix: swapping the characters and padding with = lets FromBase64String decode the part to JSON with sub zoe and team qa?, and Base64Url.DecodeFromChars decodes the original part to the same JSON.

Common Mistakes​

MistakeWhat happensDo this instead
Fixing only the paddingThe - or _ still failsSwap the characters too
Fixing only the charactersThe length still failsPad with = too
Always adding ==Too much padding for some lengthsPad to the next multiple of 4
Decoding the third part of a JWT as textIt's binary signature dataDecode only the header and payload

Troubleshooting​

SymptomLikely causeFix
The error, on a JWT partBase64url instead of base64Swap and pad, or use Base64Url.DecodeFromChars
The error, on a whole tokenThe dots between the parts aren't base64 charactersSplit the token at the dots and decode one part at a time
PowerShell can't find the Base64Url typeWindows PowerShell 5.1 runs on the .NET Framework, and Base64Url arrived in .NET 9Use PowerShell 7.6, or the swap-and-pad version

FAQ​

Does the same thing happen in C#?​

Convert.FromBase64String is the same .NET method, so yes: its documentation lists the same FormatException, and C# shows it without PowerShell's MethodInvocationException around it.

Why do some JWT parts decode without the fix?​

A part with no - or _ whose length is already a multiple of 4 is valid base64 too. The common header eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 is one of them, which is why the error seems to appear at random.

Conclusion​

FromBase64String reads only standard base64: +, / and = padding. JWT parts are base64url, with - and _ and no padding, so they fail its checks. Swap the characters back and pad to a multiple of 4, or let .NET's Base64Url.DecodeFromChars do both.

Additional Resources​