Invalid length for a Base-64 char array while decryption

烂漫一生 提交于 2020-01-13 03:15:46

问题


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


回答1:


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);
    }



回答2:


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

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


来源:https://stackoverflow.com/questions/10878727/invalid-length-for-a-base-64-char-array-while-decryption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!