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 可以让基本流保持打开状态吗?


基础教程推荐
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01