Can a CryptoStream be returned and still have everything dispose correctly?
If I have a CryptoStream that I want to pass back to the user, the naïve approach would be public Stream GetDecryptedFileStream(string inputFile, byte[] key, byte[] iv) { var fsCrypt = new FileStream(inputFile, FileMode.Open, FileAccess.Read, FileShare.Read); var rmCrypto = new RijndaelManaged(); var transform = rmCrypto.CreateDecryptor(key, iv); var cs = new CryptoStream(fsCrypt, transform, CryptoStreamMode.Read); return cs; } I know that when I dispose the CryptoStream the underlying FileStream will also be disposed . The issue I am running in to is what do I do with rmCrypto and transform ?