Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causing this "Invalid length for a Base-64 char array"

I have very little to go on here. I can't reproduce this locally, but when users get the error I get an automatic email exception notification:

Invalid length for a Base-64 char array.    at System.Convert.FromBase64String(String s)   at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString)   at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState)   at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState)   at System.Web.UI.HiddenFieldPageStatePersister.Load() 

I'm inclined to think there is a problem with data that is being assigned to viewstate. For example:

List<int> SelectedActionIDList = GetSelectedActionIDList(); ViewState["_SelectedActionIDList"] = SelectedActionIDList; 

It's difficult to guess the source of the error without being able to reproduce the error locally.

If anyone has had any experience with this error, I would really like to know what you found out.

like image 822
Slim Avatar asked May 13 '09 15:05

Slim


People also ask

How do you fix invalid length for a Base64 char array or string?

Solution for the “Invalid length for a Base-64 char array or string” error. Implement the code block below to check the string length and add padding characters if needed: int mod4 = encryptedtext. Length % 4; if (mod4 > 0 ) { encryptedtext += new string('=', 4 - mod4); } byte[] encryptedBytes = Convert.

What characters are invalid in Base64?

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 '/'. % is not allowed in base64 encoding. Save this answer.

How do you find the length of a Base64?

Each Base64 digit represents exactly 6 bits of data. So, three 8-bits bytes of the input string/binary file (3×8 bits = 24 bits) can be represented by four 6-bit Base64 digits (4×6 = 24 bits). This means that the Base64 version of a string or file will be at least 133% the size of its source (a ~33% increase).

Is there a limit to Base64 encoding?

Since there are only 26 letters in the English alphabet, a maximum of four binary bytes can be represented with three characters from the alphabet. This is where the '64' in Base64 encoding comes from - it can represent up to 64 different combinations using three out of six possible characters.


2 Answers

After urlDecode processes the text, it replaces all '+' chars with ' ' ... thus the error. You should simply call this statement to make it base 64 compatible again:

        sEncryptedString = sEncryptedString.Replace(' ', '+'); 
like image 162
Jalal El-Shaer Avatar answered Sep 17 '22 15:09

Jalal El-Shaer


I've seen this error caused by the combination of good sized viewstate and over aggressive content-filtering devices/firewalls (especially when dealing with K-12 Educational institutions).

We worked around it by storing Viewstate in SQL Server. Before going that route, I would recommend trying to limit your use of viewstate by not storing anything large in it and turning it off for all controls which do not need it.

References for storing ViewState in SQL Server:
MSDN - Overview of PageStatePersister
ASP Alliance - Simple method to store viewstate in SQL Server
Code Project - ViewState Provider Model

like image 29
Jimmie R. Houts Avatar answered Sep 21 '22 15:09

Jimmie R. Houts