This is the scenario we have: We have huge encrypted files, in the order of gigabytes that we can decrypt correctly if we read them until the end. The problem arises when we ar
Close
calls Dispose(true)
which calls FlushFinalBlock
which throws the exception, because this is not really the final block.
You can prevent this by overriding the Close
method so that it doesn't call FlushFinalBlock
:
public class SilentCryptoStream : CryptoStream {
public SilentCryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode) :
base(stream, transform, mode) {
}
public override void Close() {
this.Dispose(false);
GC.SuppressFinalize(this);
}
}
(You also need to manually close the underlying stream.)
is it valid to check if reader.EndOfStream is false and then capture the CryptographicException
I think it's OK.