Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#mvc中使用解密和加密时出现错误数据_C#_.net_Encryption - Fatal编程技术网

在c#mvc中使用解密和加密时出现错误数据

在c#mvc中使用解密和加密时出现错误数据,c#,.net,encryption,C#,.net,Encryption,我正在用C#使用解密和加密,得到了这个错误 例外情况详情: System.Security.Cryptography.Cryptography异常:错误数据 源错误: 第64行:加密流cs=新加密流(ms, des.CreateDecryptor(DecryptKey,IV),CryptoStreamMode.Write);第65行: cs.Write(inputByte,0,inputByte.Length);第66行: cs.FlushFinalBlock();第67行:System.Tex

我正在用C#使用
解密
加密
,得到了这个错误

例外情况详情: System.Security.Cryptography.Cryptography异常:错误数据

源错误:

第64行:加密流cs=新加密流(ms, des.CreateDecryptor(DecryptKey,IV),CryptoStreamMode.Write);第65行: cs.Write(inputByte,0,inputByte.Length);第66行:
cs.FlushFinalBlock();第67行:System.Text.Encoding encoding=System.Text.encoding.UTF8;第68行:返回 encoding.GetString(ms.ToArray())

我如何解决这个问题;这是我的密码

  public static string Encrypt(string plainText)
        {
            string key = "amir100amir*&%$#";
            byte[] EncryptKey = { };
            byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };
            EncryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByte = Encoding.UTF8.GetBytes(plainText);
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(EncryptKey, IV), CryptoStreamMode.Write);
            cStream.Write(inputByte, 0, inputByte.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray()).Replace("/", "_").Replace("+", "-");


        }


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

               return (Encrypt(thisEncode));

        }

        public static string decodeSTROnUrl(string thisDecode)
        {
            //thisDecode = thisDecode.Replace(" ", "+");
             // return Decrypt(HttpUtility.UrlDecode(thisDecode));
            return Decrypt((thisDecode));
        }


        public static string Decrypt(string encryptedText)
        {
           // try
            {

                encryptedText = encryptedText.Replace("_", "/");
                encryptedText = encryptedText.Replace("-", "+");
                string key = "amir100amir*&%$#";
                byte[] DecryptKey = { };
                byte[] IV = { 55, 34, 87, 64, 87, 195, 54, 21 };
                byte[] inputByte = new byte[encryptedText.Length];
                DecryptKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByte = Convert.FromBase64String(encryptedText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(DecryptKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByte, 0, inputByte.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());



            }
            //catch
            //{

            //}
            //finally { }
            //return "Error";
        }

哪一行代码产生了错误?如何调用这些函数似乎有效。你能提供更多细节吗?代码对我有用。但我使用的是基本的ascii字符串;加密“meep_meep”并对其解密会产生预期的行为。我们能看到OP加密的字符串是什么吗?也许它包含非标准字符。似乎也适用于中文
極秘cs.FlushFinalBlock();是错误吗