Base64 encoded String that contains exclamation points causing exception

1

Unfortunately, I cannot post the string as it contains sensitive data. I have created an API that is in use at my company. We have a partner that is attempting to use said API. In a part of the JSON there we expect a base64 encoded string of a digitally signed XML file. When I parse the JSON and try to decode the Base64 string, the API throws an exception.

System.FormatException occurred HResult=0x80131537
Message=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.

Source=mscorlib

StackTrace:
at System.Convert.FromBase64_Decode(Char* startInputPtr, Int32 inputLength, Byte* startDestPtr, Int32 destLength)
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64String(String s)
at Base64Decoding.Program.Main(String[] args) in

I have tried taking the raw XML and encoding it on 3 different machines, including a Linux system using Python, I have gotten the exact same Base64 string each time. The string that I receive does not match his string.

This is the only partner we work with that has ever had an issue with the encoding and not matter what I have tried, I cannot duplicate his results encoding the signed XML file. When I try to decode his Base64 using an online decoder, it displays an error. But when I click the decode button, it actually downloads the correctly decoded XML!

error decoding due to malformed string

When I use my encoded string of the same file, it does not display the error, it displays the decoded XML in the 'live view' box and downloads the correct XML when I click the decode button.

Does anyone have any idea what might cause System.Convert.ToBase64String() to output a string with exclamation points in it? To my understanding that should not be an allowed character in Base64. I have tried 64 vs. 32 bit, I have tried every version of the .Net Framework to back to 2.

c#
encoding
base64
asked on Stack Overflow Oct 19, 2017 by Robert Kaucher • edited Oct 19, 2017 by Filburt

1 Answer

0

Try replacing every exclamation point character '!' with a forward slash character '/'.

I have seen base64 encoded data with exclamation marks in it before. Usually it was embedded in HTML or was intended to be embedded in HTML. Presumably, because forward slashes are a syntactically meaningful character in HTML, and because base64 encoded data can have forward slashes in it, all the forward slashes have been replaced by an exclamation point.

I've seen people who have embedded WebAssembly programs in javascript tags do this.

As an additional note, if you have a piece of base64 encoded data, especially one that might be truncated or only part of the full base64 encoded string, and the framework is giving you an exception like the one above or an exception that says "Invalid length for a Base-64 char array or string", you can try either removing characters from the end of the string, one-by-one, trying to decode it each time (up to about 4 characters or so--beyond that its probably not a length/padding problem), or by adding padding characters '=' (equals symbol) one-by-one, but not more than twice (for a total of two equals, ie '==').

answered on Stack Overflow Sep 27, 2020 by Adam White

User contributions licensed under CC BY-SA 3.0