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;
        }
求高手帮忙看看,“填充无效,无法被移除。”这句话怎么回事啊?单步我看不懂

解决方案 »

  1.   

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

  2.   

    http://blog.csdn.net/Pipi0714/archive/2010/01/26/5256603.aspx
    不知道他的问题和不和你的一样      我原来也是这个原因  导致用socket传过来就是 填充无效,无法被移除。
    在同一个命名空间就没问题
      

  3.   

    我试过,Read也可以在解密时使用。
    但是,不知道什么原因,个别的解密无论是Write还是Read都是出现“填充无效,无法被移除” 
    纠结