在解码/解密期间,基64字符数组的长度无效

时间:2022-06-15 23:07:02

Q: I face the following big problem :

问:我面临的主要问题是:

from time to another i find the following exception:

我不时发现以下例外:

Invalid length for a Base-64 char array

基64字符数组的无效长度

I use encryption and decryption:

我使用加密和解密:

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(" ", "+") and this not fixes my problem at all

文本=文本。替换(“”、“+”),这根本不能解决我的问题

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

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

EDIT

编辑

  • 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.
  • 我在一个用于验证邮件的小应用程序中使用这个类。

EDIT:

编辑:

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

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

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

2 个解决方案

#1


17  

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

要解决这些问题,您需要先对所有就绪的Encode -base64字符串进行编码,然后解码,这取决于您使用它的位置。

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.

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

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 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.

我有同样的问题,试着用你说的替换“+”和其他,但最后这是使它起作用的东西。

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

不要忘记删除text = text。替换(“”、“+”)和其他来自代码的加密操作,只需加密和解密即可。

#2


-2  

string imag = img;
imag = imag.Replace("\", "");
int c = imag.Length % 4;
if ((c) != 0)
    imag = imag.PadRight((imag.Length + (4 - c)), "=");
[] converted = Convert.FromBase64String(imag);
using (System.IO.MemoryStream vstream = new System.IO.MemoryStream(converted)) {
    return Image.FromStream(vstream);
}

#1


17  

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

要解决这些问题,您需要先对所有就绪的Encode -base64字符串进行编码,然后解码,这取决于您使用它的位置。

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.

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

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 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.

我有同样的问题,试着用你说的替换“+”和其他,但最后这是使它起作用的东西。

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

不要忘记删除text = text。替换(“”、“+”)和其他来自代码的加密操作,只需加密和解密即可。

#2


-2  

string imag = img;
imag = imag.Replace("\", "");
int c = imag.Length % 4;
if ((c) != 0)
    imag = imag.PadRight((imag.Length + (4 - c)), "=");
[] converted = Convert.FromBase64String(imag);
using (System.IO.MemoryStream vstream = new System.IO.MemoryStream(converted)) {
    return Image.FromStream(vstream);
}