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

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

问题描述

正如标题所说,我越来越:

As the title says, I am getting:

无效长度为Base-64字符
  数组。

Invalid length for a Base-64 char array.

我已经在这里了解这个问题,似乎
建议是存储的ViewState在SQL如果它是大的。我是
使用向导具有良好的交易数据的收集因此机会
是我ViewSate大。但是,在我转向了店中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 ViewSate 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);
        }

的加密方法是这样的:

The Encrypt method looks like this:

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

下面是个什么HTML看起来像Hotmail的:

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.

<一个href=\"http://localhost:1563/Accounts/VerifyEmail.aspx?a=YOHY57xYRENEOu3H+FGq1Rf09AZAI56EPjfwuK8XWKg=\">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");

下面是getter方法​​UserNameToVerify:

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

可以这样错误地用code修复补救,或我必须存储ViewState的数据库?

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

在此先感谢。

推荐答案

一个base64连接codeD字符串的长度始终是4的倍数。如果不是4的倍数,那么 = 字符追加到它。格式为:?名称=值的查询字符串有问题时,包含 = 字数(其中一些将被丢弃,我不记得确切的行为)。您可以逃脱做的base64德code之前追加的 = 字符正确的号码。

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

当然,调用UrlEn code(如LukeH的答案)应该让这一切都毫无意义。

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

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

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