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

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

问题描述

问:我面临以下大问题:

Q: I face the following big problem :

我不时发现以下异常:

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

我使用加密和解密:

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.

编辑

  • 如果有任何修改或对该类的增强以使其成为工作更好或更安全或避免像这样的任何可能的问题,我将不胜感激.
  • 如果有交替类而不是这个,更多安全并且不会使这些问题,我将不胜感激.
  • 我使用这个类在一个小的用于验证邮件的应用程序.

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

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

推荐答案

要解决的问题是先Encode再Decode准备好的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 用作代码字符的相同字符与加密字符混合在一起.

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天全站免登陆