从CryptoStream.cs (ln 695):
protected override void Dispose(bool disposing) {
try {
if (disposing) {
if (!_finalBlockTransformed) {
FlushFinalBlock();
}
_stream.Close();
}
}
finally {
try {
// Ensure we don't try to transform the final block again if we get disposed twice
// since it's null after this
_finalBlockTransformed = true;
// we need to clear all the internal buffers
if (_InputBuffer != null)
Array.Clear(_InputBuffer, 0, _InputBuffer.Length);
if (_OutputBuffer != null)
Array.Clear(_OutputBuffer, 0, _OutputBuffer.Length);
_InputBuffer = null;
_OutputBuffer = null;
_canRead = false;
_canWrite = false;
}
finally {
base.Dispose(disposing);
}
}
}
正如你可以看到你应该调用FlushFinalBlock
方法是公共的,如果你不想处理一个CryptoStream
。此方法清除输入和输出缓冲区,因此在使用的CryptoStream
中不存储敏感信息。
GC可能会关闭底层的Stream
?不。为此,必须使用true
作为参数值调用Dispose
方法,但这只能在Stream.Close
方法(从Stream.Dispose
调用)中完成。即使CryptoStream
将实现终结器,在执行Finalize
时,在引用的对象上调用Dispose
也不是好习惯。终结器应该仅用于释放非托管资源。
你确定你需要基础流保持打开吗?因为它是这样设计的。 –
最安全的方法是在处理cryptostream之后加密到内存流并使用其缓冲区。建议少量数据。 –
@HenkHolterman是的。我需要散列一些流,并继续处理其余的没有散列CryptoStream。 – ispiro