解码/解密期间Base-64字符数组的长度无效 [英] Invalid length for a Base-64 char array during decoding/decryption

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

问题描述

问:我面对以下大问题:

Q: I face the following big problem :

从时间到另一个我发现以下异常:

from time to another i find the following exception:

Base-64 char数组的长度无效

我使用加密和解密:

public static string Encrypt(string text)
        {

            try
            {
                key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] byteArray = Encoding.UTF8.GetBytes(text);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream,des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cryptoStream.Write(byteArray, 0, byteArray.Length);
                cryptoStream.FlushFinalBlock();
                return Convert.ToBase64String(memoryStream.ToArray());
            }

            catch (Exception ex)
            {
              string message =  ex.Message;
            }

            return string.Empty;
        }



        public static string Decrypt(string text)
        {
            try
            {
                key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                text = text.Replace(" ", "+")
                Byte[] byteArray = Convert.FromBase64String(text);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream,
                des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                cryptoStream.Write(byteArray, 0, byteArray.Length);
                cryptoStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }

            catch (Exception ex)
            {
                string message = ex.Message;

            } 

我阅读了许多关于问题的文章
一些关于解决方案的帖子是:

i read many articles about the problem some posts talking about the solution is:

text = text.Replace(,+)
,这不能解决我的问题所有

text = text.Replace(" ", "+") and this not fixes my problem at all

我的字符串是: 3DZF / NZpp0yuQ = 3D
请帮我解决这个问题。

my string is :3DZF/NZpp0yuQ=3D please i need help to fix this problem.

编辑


  • 如果有任何修改或
    增强这个类,使它
    工作更好或更安全或避免
    任何可能的问题,像这样,我
    会很感激。

  • 如果有交替的课程而不是这样,更多的
    安全,不会使这些
    的问题,我将不胜感激。

  • 我在一个用于验证邮件的小
    应用程序中使用这个类。

  • If there are any modifications or enhancements to this class to make it work better or more secure or avoid any possible problems like this , i will be grateful.
  • If there are alternating classes instead of this,more secure and doesn't make these problems , I will be grateful.
  • I use this class in a small application used to verifying mails.

编辑:

Decoding the querystring values is done already when it's parsed into the Request.

https ://stackoverflow.com/a/10879400/418343

推荐答案

要解决您需要的问题,然后解码所有就绪的encode-base64字符串,取决于你使用它的位置。

To solve the problems you need to fist Encode and then Decode the all ready encode-base64 string, depend from where you using it.

如果你使用它在url(或查询)上,可能这是你要使用的地方,那么您需要在使用URL之前对其进行编码,然后在重新获取URL之前解码该URL。原因是您需要避免将URL用作代码字符的相同字符与加密字符混合。

If for example you use it on url (or query) where probably this is the place you going to use, then you need to Encode the URL before you use it, decode the url before you get it back. The reason is that you need to avoid to mix the same characters that URL use as code character, with the encrypted characters.

无论如何,这里是解决您的问题的代码(我使用的原因相同):

Anyway here is the code that solve your problem (and I use for the same reason):

public static string encodeSTROnUrl(string thisEncode)
{
    if (null == thisEncode)
        return string.Empty;

    return HttpUtility.UrlEncode(Encrypt(thisEncode));
}


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

ps
我有同样的问题,并尝试像说替换'+'等等,但最后这是什么使它工作。

ps I have the same problem, and have try as you say replace the '+' and other, but at the end this is what make it works.

不要忘记删除 text = text.Replace(,+)和其他加密操作你的代码,只是加密和解密。

Don't forget to remove the text = text.Replace(" ", "+"), and other manipulations of the encryption from your code, just encrypt and decrypt.

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

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