Anyname Donotcare
Anyname Donotcare

Reputation: 11403

Invalid length for a Base-64 char array while decryption

I get the following exception in some cases through (decryption) , and i can't recognize exactly the reason :

Invalid length for a Base-64 char array

My Code :

public static string encodeSTROnUrl(string thisEncode)
{
  if (null == thisEncode)
      return string.Empty;

  return HttpUtility.UrlEncode(Encrypt(thisEncode));
}


// string thisDecode = "3Dn%2bsJJPXprU4%3d"; //this is the value which cause the exception.
public static string decodeSTROnUrl(string thisDecode)
{
   return Decrypt(HttpUtility.UrlDecode(thisDecode));
}


QueryStringEncryption.Cryptography.decodeSTROnUrl(Request.QueryString["val"].ToString());

The exact line which throw the exception is :

 Byte[] byteArray = Convert.FromBase64String(text);

I thought i fix this problem by encoding and decoding before and after the encryption and the decryption operation.but some values still throw this exception .


Note: i note some strange behavior : the id as a query string sent to my mail is : n%2bsJJPXprU4%3d and it works without exceptions ..

and the user who has the problem the sent url contains 3Dn%2bsJJPXprU4%3d

is this a browser problem ??!!

Upvotes: 4

Views: 7995

Answers (2)

Damith
Damith

Reputation: 63065

Decoding the querystring values is done already when it's parsed into the Request. try without 'HttpUtility.UrlDecode'

public static string decodeSTROnUrl(string thisDecode)
    {
        return Decrypt(thisDecode);
    }

Upvotes: 8

Blachshma
Blachshma

Reputation: 17395

The 64-bit encoding has problems with spaces in the string. Try to add the following after encrypting

sEncryptedString = sEncryptedString.Replace(' ', '+');

Upvotes: 4

Related Questions