Can a CryptoStream leave the base Stream open?(CryptoStream 可以让基本流保持打开状态吗?)
问题描述
我创建了一个 MemoryStream
,将它传递给 CryptoStream
进行写入.我希望 CryptoStream
进行加密,并让 MemoryStream
保持打开状态,然后再读入其他内容.但是一旦CryptoStream
被处理,它也会处理MemoryStream
.
I create a MemoryStream
, pass it to CryptoStream
for writing. I want the CryptoStream
to encrypt, and leave the MemoryStream
open for me to then read into something else. But as soon as CryptoStream
is disposed, it disposes of MemoryStream
too.
CryptoStream
可以让基础 MemoryStream
以某种方式打开吗?
Can CryptoStream
leave the base MemoryStream
open somehow?
using (MemoryStream scratch = new MemoryStream())
{
using (AesManaged aes = new AesManaged())
{
// <snip>
// Set some aes parameters, including Key, IV, etc.
// </snip>
ICryptoTransform encryptor = aes.CreateEncryptor();
using (CryptoStream myCryptoStream = new CryptoStream(scratch, encryptor, CryptoStreamMode.Write))
{
myCryptoStream.Write(someByteArray, 0, someByteArray.Length);
}
}
// Here, I'm still within the MemoryStream block, so I expect
// MemoryStream to still be usable.
scratch.Position = 0; // Throws ObjectDisposedException
byte[] scratchBytes = new byte[scratch.Length];
scratch.Read(scratchBytes,0,scratchBytes.Length);
return Convert.ToBase64String(scratchBytes);
}
推荐答案
您可以使用 using 语句.您将需要手动管理对象的处置,您还需要调用 FlushFinialBlock()
以确保所有数据在处理之前都已写入底层流.
You can but you will not be able to use using statements. You will need to manually manage the disposing of the object and you will also need to call FlushFinialBlock()
to make sure all the data was written out to the underlying stream before working on it.
一旦您完成了对流的所有工作,您就可以处理在最后的 finally 块中等待的所有资源.
Once all you are done working with the stream you can then dispose all of the resources you where waiting on in the finally block at the end.
MemoryStream scratch = null;
AesManaged aes = null;
CryptoStream myCryptoStream = null;
try
{
scratch = new MemoryStream();
aes = new AesManaged();
// <snip>
// Set some aes parameters, including Key, IV, etc.
// </snip>
ICryptoTransform encryptor = aes.CreateEncryptor();
myCryptoStream = new CryptoStream(scratch, encryptor, CryptoStreamMode.Write);
myCryptoStream.Write(someByteArray, 0, someByteArray.Length);
//Flush the data out so it is fully written to the underlying stream.
myCryptoStream.FlushFinalBlock();
scratch.Position = 0;
byte[] scratchBytes = new byte[scratch.Length];
scratch.Read(scratchBytes,0,scratchBytes.Length);
return Convert.ToBase64String(scratchBytes);
}
finally
{
//Dispose all of the disposeable objects we created in reverse order.
if(myCryptoStream != null)
myCryptoStream.Dispose();
if(aes != null)
aes.Dispose();
if(scratch != null)
scratch.Dispose();
}
这篇关于CryptoStream 可以让基本流保持打开状态吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CryptoStream 可以让基本流保持打开状态吗?
基础教程推荐
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 创建属性设置器委托 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01