在c#mvc

时间:2016-02-19 10:42:23

标签: c# .net encryption

我正在使用DecryptEncrypt使用C#并收到此错误

  

异常详细信息:   System.Security.Cryptography.CryptographicException:Bad Data。

     

来源错误:

     

第64行:CryptoStream cs = new CryptoStream(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";
        }

0 个答案:

没有答案