Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ??!!

like image 573
Anyname Donotcare Avatar asked Dec 16 '22 23:12

Anyname Donotcare


1 Answers

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);
    }
like image 152
Damith Avatar answered Dec 21 '22 23:12

Damith