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

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

问题描述

问:我面临以下大问题:

Q: I face the following big problem :

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

from time to another i find the following exception:

长度无效的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.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.

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

推荐答案

要解决,你需要拳头恩code中的问题,然后德code的都准备好了EN code-的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(或查询),其中大概这就是你要使用的地方使用它,那么你需要连接code中的网址,你使用它c中的网址,去$ C $前前你找回来。其原因是,你需要避免混合网址为code字使用相同的字符,与加密字符。

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.

反正这里是code,以解决您的问题(我用出于同样的原因):

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.Replace(,+),然后从你的code加密等操作,只是加密和解密。

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