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.

Most Commented Post

One Response to “Base-64 as URL Parameter”

  • 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.

    Thanks,
    Varun Kumar.

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear