Home » none » Base-64 as URL Parameter

Base-64 as URL Parameter

other-096 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.

Related Post

  • Overriding Events in VB.NETOverriding 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.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 HKLMUnauthorizedAccessException 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) k […]
  • How to properly access controls in the Master PageHow 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.configUnable 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

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • Varun Kumar

    Hi ..

    This finally helped me to get out of problem. I already had a solution, but this gave good picture of the problem.

    Varun Kumar.