Getting a few different exceptions related to Base-64 strings in C#. Below are the exceptions along with sample string. I don't know too much about this topic but I believe I did confirm the lengths of the following strings are multiples of 4. I'm hoping these issues are all related but I'm not sure. Any help is appreciated.
"Invalid character in a Base-64 string."
DcxWhbe30oLrUI69U41gSnm7eU2c8qOx7k/eKnSaG3ZDlv8BIPHJKxl0DStVUtsjdxufrZd30K4IAl0XxBIKgKUlOt4k+nVPrfCnM4HHzw19GHnYlTr2DLhw6iD0goEnMk+bJG2xWQ4wRLLn8KOj6DdBxTes6sdpoeWKxcAHPZ2FFXqC3MfQ+SMMV7re9YiY9ZpudFFr1QVRMztzinK5YA==
"Length of the data to decrypt is invalid."
jq6xC0Dv+oeL0MXOei7wNIQgD+aon+S8N2ZfDyAO+5dlm/lIBYN+aCn2Xjnyi/XCzv7Liag5aaKXZ3b+7P7+R7lmarsT9ugnFrnvjInsvUxsbk4EAjIP55O9d7XXYnARW3yjW7klLyR0U9JjMVsISKThDtLe4rOJHgDvYahF454YOn5GlVXBoB+qNnLscGZ6
"Invalid length for a Base-64 char array."
C8MutyUPoTrmmnprFV3wqxMMVs7O5J4NA61jhZLaCSbwteklqGHFuuSlhf0mH8L5FLJ+jcrWuns2zz+Q/o5tA0DCDGnEAxDxKraR8Ip/iDUrTCS9ZAW0VrZpqUY4EPhyCbSpdU0OwfQ05eHXqwU6LIzz3gqavpi6AlMP9H5GYTaxsfQEByswjizWs5hO2ZmH
As I went to look at what I was using to decode the string in response to Andrey's question, I realized that I had been making an assumption that was blinding me from the real issue.
The data string is being passed through a querystring parameter and I was assuming that parameter as seen in the custom error message I was getting was the bad string when, in fact, a little debugging showed that the string I was actually attempting to decode had whitespaces. Posting the string through the querystring was causing all the plus (+) signs to get decoded into whitespace. I am able to resolve this issue from there.
Thank you to anyone who spent time reading this rather misguided question.
Related
Here is the input query string that I enter on the Address Bar:
https://myapp.com/myproject/exagoviewer.aspx?act_code=R&rep_own=AAAAAAAAA&report_name=3.52%20SR%20-%20Request%20Time%20Analysis&report_code=3.52%20SR
Then, in my code, when I get its query string by calling Page.ClientQueryString(), it returns this string
act_code=R&rep_own=AAAAAAAAA&report_name=3.52+SR+-+Request+Time+Analysis&report_code=3.52%2520SR
You can see there is a weir encode string in the parameter report_code=3.52%2520SR, it should be report_code=3.52%20SR
Then I tried calling another method HttpContext.Current.Request.Url.Query to get the query string, but I got the same issue:
?act_code=R&rep_own=AAAAAAAAA&report_name=3.52+SR+-+Request+Time+Analysis&report_code=3.52%2520SR
I have no idea what the problem I am facing now. Any advice, please?
Nevermind, I found the root cause. It is caused by the client had called encodeURIComponent() one more time before that request came to the server. It means the report_code's value was encoded twice and it converted % to %25
Consider the following line of code:
string _decoded = System.Web.HttpUtility.UrlDecode(
"There%20should%20be%20text%20after%20this%0022help!");
The encoded line
"There%20should%20be%20text%20after%20this%0022help!"
when decoded via the website urldecoder.org produces
"There should be text after this22help!"
however the value of _decoded as displayed in the debugger is:
Figure 1: Debugger view of problem
What could be causing this problem? Is there a setting or special encoding that will circumvent this in all cases?
EDIT: Yes, I consider this behavior to be an error. I don't want URLDecode to introduce the \0 char to the resultant string, because it would result in an invalid file name (my code is moving around files).
There is a null byte (\0 = %00) after this so the debugger doesn't show the rest of the string.
So the decoded value is correct, it's just the limitation (or bug?) of the debugger.
You can take a look at here for more info about null byte from security perspective. And there is this question posted about it as well.
Hello thank you for reading;
I've tried to decrypt thise Base64 string in anyway possible. Also tried searching Stackoverflow and tried other methods. Every response it's just jibberish.
v6kEwElTQI%2fNlQc87zM7Od2%2fsaAghvSbCVyYaJRTf4U%3d
Hope you've got any ideas!
It's not exactly readable, but it does successfully Base64 decode:
Firstly, I manually de-URL-encoded it. There are three URL escape sequences in there:
%2f : '/' (two of these)
%3d : '='
Giving me v6kEwElTQI/NlQc87zM7Od2/saAghvSbCVyYaJRTf4U=
With those changed, it's Base64.
Then, you can use the old methods of decoding in C# .NET:
byte[] arr = Convert.FromBase64String('v6kEwElTQI/NlQc87zM7Od2/saAghvSbCVyYaJRTf4U=');
Console.WriteLine(System.Text.Encoding.Default.GetString(arr));
// As a note, I'm using Default encoding here. Your system may use
// a different encoding by default. Alternatively, you can replace
// 'Default' here with ASCII or Unicode.
Which gave me ¿©ÀIS#Í•<ï3;9Ý¿± †ô› \˜h”S…
Using unicode I get 쀄卉轀闍㰇㏯㤻뿝ꂱ蘠鯴尉梘厔蕿 instead.
I have ruled out that it's not an image (at least in a System.Drawing.Image format.)
While decoding it was fun, I have to agree with the comments. It's important to know what the data is going to be after it's decoded since it'll only be a nameless stream of bytes at that point.
I receive this exception during parsing string containing JSON:
Newtonsoft.Json.JsonReaderException: Unterminated string. Expected delimiter: ". Path '[114].var2', line 1, position 431602.
So I went to exactly that (431602) position and found that it's here:
(...)lZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqK*jp*KWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW1(...)
So it's just simply "jp" chars which shouldn't be anything wrong.
What could be a reason for this exception?
EDIT
To be more specific I also put a whole string with few variables around it:
"var1":"946","var2":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/\/gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gNjAK\/9sAQwANCQoLCggNCwoLDg4NDxMgFRMSEhMnHB4XIC4pMTAuKS0sMzpKPjM2RjcsLUBXQUZMTlJTUjI+WmFaUGBKUVJP\/9sAQwEODg4TERMmFRUmTzUtNU9PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09P\/8AAEQgAeABHAwEiAAIRAQMRAf\/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC\/\/EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFh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DGPYlj\/n8a2LjxnZKD5FvPJ\/vYUf1rHn8Z3jt+4too\/wDeJY\/0rJSxL+GKRVqa3dzctfB9lHg3E80p9BhR\/X+dalloel\/bkt4LWLcBuct8zBfXmuPW58R6mm5Tc7D\/AHR5a\/nxXoXhHQjounE3BDXlwd0zZzj0XPt\/PNOFCpOXvzv5ITmktEboAVQqjAAwAKWiivQMQooooA8417wlc3OsX00U0McUkvmDOc8gE8Y9SazIfCUCNi5upH9kUL\/PNdZ411S60ia3e3ijdLhSpLg8Ffoff9K8+udc1i7lKwswz2hj\/wAmuKca7k7OyNouCWqOpt9A0mFMm3Lkd3cn\/wCtT2uNF0\/7rWkLDsoG79Oa5BNL1q95m8zB7zSf061p6d4OmnuESa5XJONsa5\/U1g6Uft1LlqT6RO18OXUWsXLSW6u1tbEZkYYDP2A+nX8q6qqek6bb6Tp0VlajCRjknqx7k1crvpUo0o2iYSk5O7CiiitSQooooAoavaLc26ExLI0ThgCufY\/zrEOi39xIf3ccMeeNx7fQUUVzzoRqTvIuM3FaFqLw1ggy3ZI7hEx+pP8AStOy0u2sm3xb2fGNznOKKKuNGnHVITnJ7su0UUVqSJRmiigAzRRRQB\/\/2Q==","var3":"77241"
I noticed that this seems to be a base64 encoded JPEG. In most cases it would be better & easier to have your server send back the jpeg rather than JSON.
public ActionResult ShowImage()
{
var file = Server.MapPath("~/App_Data/UserUpload/asd.png");
return File(file, "image/png", Path.GetFileName(file));
}
To see original source follow this link.
Troubleshooting advice:
If you are unable to put the whole string, you may want to make it smaller to isolate the error.
Try removing a a block characters at a time (I would use a binary left/right half experimentation method) until it stops breaking. Then look closer at the bad half. If you can get the bad half small enough and are unable to see the error, please post it.
I am guessing the column given by the error isn't counted the same way that you are counting them.
I am trying to make a connection via HttpWebRequest. I am reading url links from a text file.
However, I am getting 'System.UriFormatException' error.
I have tried to add http:// in front of the url links in my text file. But the result is the same.
What can be the solution?
If the error only occurs when reading the values from a file, but not when assigned directly to a string variable, then I'm guessing that there are extra characters being read (quotes, escaped characters, carriage-return/line feeds, etc).
Try reading the first value into a string, and then comparing that to what you expect in the explicit string value. Any differences should be obvious after that.
Without seeing code it's impossible to tell what you're trying to do. But you can start by looking at the Message property of the UriFormatException class. Message is a string that explains the reason for the exception.