Base-64 as URL Parameter
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.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!


December 6th, 2008 at 7:56 am
Hi ..
This finally helped me to get out of problem. I already had a solution, but this gave good picture of the problem.
Thanks,
Varun Kumar.