Dercyption错误“填充无效且无法删除”

时间:2014-05-03 15:22:22

标签: c#-4.0

嘿,每一个这是我的解密代码,我想解密这个密码,但问题是它给出了错误

 static void Main(string[] args)
        {

            string str = Decrypt("vASqxLq4dmegE0l3K8T7ng==", "");
            Console.WriteLine(str);
            Console.ReadKey();
        }

        private const string IV = "MazenTech.com.pk";
        private const int keysize = 128;
        public static string Decrypt(string cipherText, string passPhrase)
        {
            byte[] plainTextBytes = null;
            int decryptedByteCount = 0;
            try
            {
                byte[] IVBytes = Encoding.ASCII.GetBytes(IV);
                byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
                PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
                byte[] keyBytes = password.GetBytes(keysize / 8);
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, IVBytes);
                MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                plainTextBytes = new byte[cipherTextBytes.Length];
                decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); // error this line 
                memoryStream.Close();
                cryptoStream.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Your Password is Incorrect....");
            }
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);

        }

如何解决它请帮助我。

1 个答案:

答案 0 :(得分:0)

通过设置PeddingMode属性来尝试。

RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.None;