Yesterday, I was working on an application that passes encrypted data as a URLEncoded parameter on to another page.
The problem we were having with this code was that the code would occasionally give us the error, “Invalid Length for a Base-64 char array” when the code was converting the parameter back to a byte array so that it could decrypt it.
What was even more puzzling is that the exact same code run from two different computers would produce two different results. So it wasn’t exactly data-specific.
As we were looking into the problem further, we noticed that the only difference between the URLs that were working and the URLs that were not is that the character sequence %2b had been replaced by a plus sign in the browser’s address bar. It turns out that the character sequence %2b is the URLEncoded value for the plus sign. And the plus sign is the URLEncoded value for a space.
But a space character doesn’t belong in a Base-64 encoded string. It is an invalid character.
Could it be that the browser where this error is occurring is being too smart and the result is that by the time the Base-64 encoded parameter gets to the decoding code, there is a space?
Time to break out the debugger. Sure enough–that is what seems to be happening. So the simple solution is to write some replace code right before the Convert.FromBase64String() code to replace all spaces with plus signs.
Once we did that, it all worked as expected again.
Like this Article? Subscribe to get every article sent to your email.
- Overriding Events in VB.NET Back in the day, you use to be able to override an event in VB by using the drop down list in the code window. Today, I had a friend who is moving from Visual Studio 2003 to Visual […]
- ASP.NET AJAX using JSON – Here’s how. Last week I wrote a post about how simple JSON is. In it I explained the main differences between using JSON and using the update panel. I really started out thinking I'd get to how to […]
- UnauthorizedAccessException writing to HKLM Q. I'm trying to write to HKLM on a Vista computer using the following code:Dim k As Microsoft.Win32.RegistryKey
k = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software", True)
- How to properly access controls in the Master Page One of the great new features in ASP.NET 2.0 has been the ability to use master pages to layout the common elements of the web site. This certainly works better than top and bottom […]
- Unable to find connection…for object web.config I've seen this error a couple of different times. Mostly from other people calling me with the problem. So I still don't know what ultimately causes the problem. But if you're having […]
About Dave Bush