If I have a CryptoStream that I want to pass to the user, a naive 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 host CryptoStream , the underlying FileStream will also be used. The problem I am facing is what am I doing with rmCrypto and transform ? RijndaelManaged and ICryptoTransform are one-time classes, but deleting a stream does not destroy these two objects.
What is the right way to deal with this situation?
source share