gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 03:40:14 27 4
gpt4 key购买 nike

正如标题所说,我得到:

Invalid length for a Base-64 char array.

我在这里读到了这个问题,看来如果 ViewState 很大,建议将其存储在 SQL 中。我是使用具有大量数据收集的向导,因此有机会我的 ViewState 很大吗?但是,在我转向“数据库中的存储”之前解决方案,也许有人可以看一下并告诉我是否有其他选择?

我使用以下方法构建要发送的电子邮件:

public void SendEmailAddressVerificationEmail(string userName, string to)
{
string msg = "Please click on the link below or paste it into a browser to verify your email account.<BR><BR>" +
"<a href=\"" + _configuration.RootURL + "Accounts/VerifyEmail.aspx?a=" +
userName.Encrypt("verify") + "\">" +
_configuration.RootURL + "Accounts/VerifyEmail.aspx?a=" +
userName.Encrypt("verify") + "</a>";

SendEmail(to, "", "", "Account created! Email verification required.", msg);
}

加密方法如下所示:

public static string Encrypt(string clearText, string Password)
{

byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);

PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });


byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));

return Convert.ToBase64String(encryptedData);
}

这是 hotmail 中 HTML 的样子:

Please click on the link below or paste it into a browser to verify your email account.

http://localhost:1563/Accounts/VerifyEmail.aspx?a=YOHY57xYRENEOu3H+FGq1Rf09AZAI56EPjfwuK8XWKg=

在接收端,VerifyEmail.aspx.cs 页面有一行:

 string username = Cryptography.Decrypt(_webContext.UserNameToVerify, "verify");

这是 UserNameToVerify 的 getter:

public string UserNameToVerify
{
get
{
return GetQueryStringValue("a").ToString();
}
}

这是 GetQueryStringValue 方法:

private static string GetQueryStringValue(string key)
{
return HttpContext.Current.Request.QueryString.Get(key);
}

解密方法如下:

public static string Decrypt(string cipherText, string password)
{

**// THE ERROR IS THROWN HERE!!**
byte[] cipherBytes = Convert.FromBase64String(cipherText);

这个错误可以通过代码修复来补救,还是我必须将 ViewState 存储在数据库中?

最佳答案

base64 编码字符串的长度始终是 4 的倍数。如果它不是 4 的倍数,则附加 = 字符直到它是。当 value 包含 = 字符时,?name=value 形式的查询字符串会出现问题(其中一些将被删除,我不不记得确切的行为)。在进行 base64 解码之前,您可以附加正确数量的 = 字符。

编辑 1

您可能会发现 UserNameToVerify 的值已将 "+" 更改为 "",因此您可能需要做这样的事情:

a = a.Replace(" ", "+");

这应该得到正确的长度;

int mod4 = a.Length % 4;
if (mod4 > 0 )
{
a += new string('=', 4 - mod4);
}

当然,调用 UrlEncode(如 LukeH 的回答)应该使这一切都没有实际意义。

关于c# - Base-64 字符数组的长度无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2925729/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com