C# 解密时Base-64字符数组的长度无效

C# 解密时Base-64字符数组的长度无效,c#,asp.net,exception,base64,encryption,C#,Asp.net,Exception,Base64,Encryption,在某些情况下,我通过(解密)获得以下异常,但我无法准确识别原因: Base-64字符数组的长度无效 我的代码: public static string encodeSTROnUrl(string thisEncode) { if (null == thisEncode) return string.Empty; return HttpUtility.UrlEncode(Encrypt(thisEncode)); } // string thisDecode = "3D

在某些情况下,我通过(解密)获得以下异常,但我无法准确识别原因:

Base-64字符数组的长度无效

我的代码:

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());
引发异常的确切行是:

 Byte[] byteArray = Convert.FromBase64String(text);
我想我可以通过在加密和解密操作前后进行编码和解码来解决这个问题。但是有些值仍然抛出这个异常


注意:我注意到一些奇怪的行为: 作为查询字符串发送到我的邮件的id是:
n%2bsJJPXprU4%3d
,它可以正常工作

而有问题的用户发送的url包含
3Dn%2bsJJPXprU4%3d


这是一个浏览器问题吗

64位编码在字符串中存在空格问题。 加密后尝试添加以下内容

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

当querystring值被解析到请求中时,已经完成了对它的解码。不带 “HttpUtility.UrlDecode”

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

除非空格和加号代表OP的相同字符,否则这不是正确的解决方案。@just_name,更新了响应,我记错了。您需要更改加密的结果。。。或者(甚至更好)在解密之前,重点不是要避免异常,而是要解码您最初编码的确切字符串,但这不起作用。您可以发布
加密
解密
的全部代码吗?很可能您正在进行一些不适当的编码转换(ASCII、Unicode)。此链接中的全部代码:谁会想到呢。这就是我的解决方案!谢谢@Damith!