Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c中Base-64字符数组错误的长度无效#_C#_Asp.net - Fatal编程技术网

C# c中Base-64字符数组错误的长度无效#

C# c中Base-64字符数组错误的长度无效#,c#,asp.net,C#,Asp.net,在远程服务器中,我已将此重定向添加到我的web服务器: 在index.aspx网页上,其中querystringUserNumber的值是加密的: protected void Page_Load(object sender, EventArgs e) { Response.Redirect("http://.../default.aspx?UserNumber=" + Encrypt(Request.QueryString["UserNumber"].ToString().Trim()

在远程服务器中,我已将此
重定向
添加到我的web服务器:

index.aspx
网页上,其中
querystring
UserNumber的值是加密的

protected void Page_Load(object sender, EventArgs e)
{
    Response.Redirect("http://.../default.aspx?UserNumber=" + Encrypt(Request.QueryString["UserNumber"].ToString().Trim()));
}
网页
index.aspx
在身份验证页面上重定向,其中
querystring
UserNumber的值被解密

我运行了几个测试,但我对这个用户号有问题:
a425033
a425033

UserNumber的值为
a425033
a425033
时,用于身份验证的网页会打印此错误:

错误是:

Invalid length for a Base-64 char array
下面是我的代码

请帮帮我

先谢谢你

private string Encrypt(string clearText)
{
    string EncryptionKey = "Some String";

    if (Request.QueryString["UserNumber"] != null)
    {
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { /*Some Bytes*/ });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }
    else
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('No user.');window.location='http://.../main.aspx';", true);
        return null;
    }
}

private string Decrypt(string cipherText)
{
    string EncryptionKey = "Some String";

    if (Request.QueryString["UserNumber"] != null)
    {
        byte[] cipherBytes = Convert.FromBase64String(cipherText);

        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { /*Some Bytes*/ });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                cipherText = Encoding.Unicode.GetString(ms.ToArray());
            }
        }
        return cipherText;
    }
    else
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('No user.');window.location='http://.../main.aspx';", true);
        return null;
    }
}
试试这个:

Decrypt(Request.QueryString["UserNumber"].ToString().Replace(" ", "+"))

我希望能帮助你解决问题。

无法重现。您始终可以将
Convert.ToBase64String()
的输出馈送到
Convert.FromBase64String()
,因此您不必向后者馈送base64编码的字符串。可能是因为您对加密和未加密表单都重用了相同的查询字符串参数(即
UserNumber
)。无论如何,你可能想重新考虑一下,你是否真的想自己重新发明一个叫做“身份验证”的轮子。查询字符串参数最终将出现在代理服务器和浏览器历史记录中。非常非常不安全。Base64编码的字符串不能直接包含在URL查询字符串中,因为它们包含被解释为特殊的字符(例如“+”)。在将Base64数据放入查询字符串之前,需要对其进行URL编码,或者使用Base64的URL安全变体。