Base-64 字符数组的长度无效 [英] Invalid length for a Base-64 char array

查看:37
本文介绍了Base-64 字符数组的长度无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我得到:

As the title says, I am getting:

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

Invalid length for a Base-64 char array.

我已经在此处阅读了有关此问题的信息,并且似乎建议是将 ViewState 存储在 SQL 中,如果它很大.我是使用具有大量数据收集的向导,因此有机会是我的 ViewState 很大.但是,在我转向store-in-DB"之前解决方案,也许有人可以看看并告诉我是否有其他选择?

I have read about this problem on here and it seems that the suggestion is to store ViewState in SQL if it is large. I am using a wizard with a good deal of data collection so chances are my ViewState is large. But, before I turn to the "store-in-DB" solution, maybe somebody can take a look and tell me if I have other options?

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

I construct the email for delivery using the below method:

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 的样子:

Here is what the HTML looks like in hotmail:

请点击以下链接或将其粘贴到浏览器中以验证您的电子邮件帐户.

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页面有一行:

On the receiving end, the VerifyEmail.aspx.cs page has the line:

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

这是 UserNameToVerify 的 getter:

Here is the getter for UserNameToVerify:

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

这里是 GetQueryStringValue 方法:

And here is the GetQueryStringValue method:

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 存储在数据库中?

Can this error be remedied with a code fix or must I store ViewState in the database?

推荐答案

base64 编码字符串的长度总是 4 的倍数,如果不是 4 的倍数,则 = 个字符被附加到它是.?name=value 形式的查询字符串在 value 包含 = 字符时出现问题(其中一些将被删除,我不不记得确切的行为).在执行 base64 解码之前,您可以通过附加正确数量的 = 字符而逃脱.

The length of a base64 encoded string is always a multiple of 4. If it is not a multiple of 4, then = characters are appended until it is. A query string of the form ?name=value has problems when the value contains = charaters (some of them will be dropped, I don't recall the exact behavior). You may be able to get away with appending the right number of = characters before doing the base64 decode.

编辑 1

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

You may find that the value of UserNameToVerify has had "+"'s changed to " "'s so you may need to do something like so:

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

这应该得到正确的长度;

This should get the length right;

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

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

Of course calling UrlEncode (as in LukeH's answer) should make this all moot.

这篇关于Base-64 字符数组的长度无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆