日期:2014-05-17  浏览次数:22108 次

解密时出现问题了--填充无效,无法被移除。
 public static bool AESDecrypttoSingle(string filePath, string strKey)//(解密文件路径,解密密码)
        {
            string dir = Directory.GetParent(filePath).ToString();
            SymmetricAlgorithm aes = Rijndael.Create();
            
            FileStream fs = File.OpenRead(filePath);
            
            aes.Key = Encoding.UTF8.GetBytes(strKey);
            aes.IV = _key1;
            byte[] decryptBytes = new byte[(int)fs.Length];
            fs.Read(decryptBytes, 0, 1);
            fs.Close();
            MemoryStream ms = new MemoryStream(decryptBytes);
            CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read);
           
            cs.Read(decryptBytes, 0, decryptBytes.Length);//填充无效,无法被移除。
            cs.FlushFinalBlock();
            fs.Close();
            fs = File.OpenWrite(dir + "\\Encryption.txt");
            foreach (byte b in ms.ToArray())
            {
                fs.WriteByte(b);

            }
            fs.Close();
                       cs.Close();
            ms.Close();
            return true;


        }



求高手帮忙看看,“填充无效,无法被移除。”这句话怎么回事啊?单步我看不懂

------解决方案--------------------
不论是加密还是解密,都应该使用 CryptoStreamMode.Write,而不是Read!