CryptoStream problem
-
Hi all. Is there a way of Closing or releasing a CryptoStream without closing the underlying stream? I've created a helper class that encrypts and decrypts streams. The problem is that if I call Clear() or Dispose() on my CryptoStream in order to release the resources its using I'm also closing the underlying Encrypted Stream I'm passing out of my Method. I could always copy the encrypted stream to a new one (MemoryStream copyStream = new MemoryStream(encryptedStream.ToArray()) and then close the CryptoStream but I cant believe thats the only solution. Or is it valid not to Close/Dispose a CryptoStream? thanks for any help.
-
Hi all. Is there a way of Closing or releasing a CryptoStream without closing the underlying stream? I've created a helper class that encrypts and decrypts streams. The problem is that if I call Clear() or Dispose() on my CryptoStream in order to release the resources its using I'm also closing the underlying Encrypted Stream I'm passing out of my Method. I could always copy the encrypted stream to a new one (MemoryStream copyStream = new MemoryStream(encryptedStream.ToArray()) and then close the CryptoStream but I cant believe thats the only solution. Or is it valid not to Close/Dispose a CryptoStream? thanks for any help.
-
You can try
Flush
. Personally I don't think that I've had a problem with opening aMemoryStream
, then aStreamWriter
on top of that, closing theStreamWriter
and I was still able to access theMemoryStream
.Hi Ed, thanks for the reply. Flush() will only make the CryptoStream write anything left in the buffer to the underlying stream but it wont release/dispose the CryptoStream itself. Normally Streams don't close an underlying stream when they are disposed. But this is not the case with Compression streams (GZipStream) or CryptoStream, where the underlying stream will get closed when you dispose the GZipStream or the CryptoStream. In the case of a GZipStream there is an easy way around it because you can directly specify in the constructor if you want to leave the underlying stream open or not when you dispose the compression stream (by default it will close it). This option however is not availabe in the CryptoStream constructor and it will always close the underlying stream when you dispose/close/clear it. I was wondering if there is anyway around this issue or if making a copy of the uderlying stream is the only way.
-
Hi Ed, thanks for the reply. Flush() will only make the CryptoStream write anything left in the buffer to the underlying stream but it wont release/dispose the CryptoStream itself. Normally Streams don't close an underlying stream when they are disposed. But this is not the case with Compression streams (GZipStream) or CryptoStream, where the underlying stream will get closed when you dispose the GZipStream or the CryptoStream. In the case of a GZipStream there is an easy way around it because you can directly specify in the constructor if you want to leave the underlying stream open or not when you dispose the compression stream (by default it will close it). This option however is not availabe in the CryptoStream constructor and it will always close the underlying stream when you dispose/close/clear it. I was wondering if there is anyway around this issue or if making a copy of the uderlying stream is the only way.
I wouldn't have thought so then, you might want to take a look through the CryptoStream's methods with Reflector, I've always found it useful. I've just taken a look through and the CryptoStream does not override the Close method so defaults to Stream's Close method which in turn calls the Dispose method, which kinda sucks X| So no it doesn't look as though you can without copying the entire stream :sigh:
-
I wouldn't have thought so then, you might want to take a look through the CryptoStream's methods with Reflector, I've always found it useful. I've just taken a look through and the CryptoStream does not override the Close method so defaults to Stream's Close method which in turn calls the Dispose method, which kinda sucks X| So no it doesn't look as though you can without copying the entire stream :sigh:
Just checked it out with Reflector and yeah you're right, there's no way around it. It'll close the underlying stream. thanks for the tip about Reflector.
-
Just checked it out with Reflector and yeah you're right, there's no way around it. It'll close the underlying stream. thanks for the tip about Reflector.