C# 在返回流函数中处理不同的流

C# 在返回流函数中处理不同的流,c#,wpf,cryptography,C#,Wpf,Cryptography,我有返回加密流的函数: public static CryptoStream CreateStream(string inputfile, string password) { byte[] passwordBytes = Encoding.UTF8.GetBytes(password); byte[] salt = new byte[32]; FileStream fsCrypt = new FileStream(inputfile,

我有返回加密流的函数:

public static CryptoStream CreateStream(string inputfile, string password)
    {
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] salt = new byte[32];

        FileStream fsCrypt = new FileStream(inputfile, FileMode.Open);
        fsCrypt.Read(salt, 0, salt.Length);

        RijndaelManaged AES = new RijndaelManaged();
        AES.KeySize = 256;
        AES.BlockSize = 128;
        var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
        AES.Key = key.GetBytes(AES.KeySize / 8);
        AES.IV = key.GetBytes(AES.BlockSize / 8);
        AES.Padding = PaddingMode.PKCS7;
        AES.Mode = CipherMode.CFB;

        return new CryptoStream(fsCrypt, AES.CreateDecryptor(), CryptoStreamMode.Read);
    }
我这样称呼它:

CryptoStream cs = FileCryptoDecryptor.CreateStream("file.xml.aes", AppPass);

在CreateStream函数中,我正在创建filestream,我的问题是何时调用
cs.Dipose()
它也处理文件流吗?

在默认情况下处理时,
CryptoStream
的构造函数有第四个可选参数。您可以将
true
作为额外参数传递,使其保持打开状态


参见此处:

尽管我同意@Stuart方法,但对于不提供一次性功能的接口,我使用包装器:

public sealed class DisposableStream : Stream
{
    public override bool CanRead => _inner.CanRead;

    public override bool CanSeek => _inner.CanSeek;

    public override bool CanWrite => _inner.CanWrite;

    public override long Length => _inner.Length;

    public override long Position { get => _inner.Position; set => _inner.Position = value; }
    private readonly IDisposable _disposable;
    private readonly Stream _inner;

    public DisposableStream(Stream inner, IDisposable disposable)
    {
        if (disposable == null)
            throw new ArgumentNullException(nameof(disposable));
        _disposable = disposable;
        _inner = inner;
    }
    public override void Flush()
    {
        _inner.Flush();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return _inner.Read(buffer, offset, count);
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        _inner.SetLength(value);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        _inner.Write(buffer, offset, count);
    }

    public override void Close()
    {
        _inner.Close();
        base.Close();
    }

    protected override void Dispose(bool disposing)
    {            
        _inner.Dispose();
        _disposable.Dispose();
        base.Dispose(disposing);
    }
}
然后,如果我想在返回函数中包装所有内容:

public Stream OpenComplexRead(string path)
{
    var a = new FileStream(path);
    var b = new DisposableStream(new CryptoSomethingStream(a), a);
    var c = new DisposableStream(new TracingStream(b), b);
    return c;
}

这将把所有东西聚集在一个地方

很乐意帮忙,请随意给它绿色支票