Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. CryptoStream problem

CryptoStream problem

Scheduled Pinned Locked Moved C#
helpquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gumi_r msn com
    wrote on last edited by
    #1

    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.

    E 1 Reply Last reply
    0
    • G gumi_r msn com

      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.

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      You can try Flush. Personally I don't think that I've had a problem with opening a MemoryStream, then a StreamWriter on top of that, closing the StreamWriter and I was still able to access the MemoryStream.

      G 1 Reply Last reply
      0
      • E Ed Poore

        You can try Flush. Personally I don't think that I've had a problem with opening a MemoryStream, then a StreamWriter on top of that, closing the StreamWriter and I was still able to access the MemoryStream.

        G Offline
        G Offline
        gumi_r msn com
        wrote on last edited by
        #3

        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.

        E 1 Reply Last reply
        0
        • G gumi_r msn com

          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.

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          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:

          G 1 Reply Last reply
          0
          • E Ed Poore

            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:

            G Offline
            G Offline
            gumi_r msn com
            wrote on last edited by
            #5

            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.

            E 1 Reply Last reply
            0
            • G gumi_r msn com

              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.

              E Offline
              E Offline
              Ed Poore
              wrote on last edited by
              #6

              gumi_r@msn.com wrote:

              thanks for the tip about Reflector.

              Saved my ass more than once :rolleyes:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups