decrypting from memory stream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JW

    #1

    decrypting from memory stream


    I can encrypt the contents of a memory stream with no problems, however when
    decrypting( I'm using the same key an IV ) I get an exception stating bad
    data.

    I'm at a lost. I'm actually creating a new memory stream, filling it with
    the encrypted bytes then trying to read from the crypto stream object. I
    have used the same approach encrypting to a file handle and then decrypting
    from the same file and it works.



    sample: (this same code works with a file stream but not memory stream) what
    am I missing?

    MemoryStream memStrEnc2 = new MemoryStream(ne wByte, 0, newByte.Length) ;

    CryptoStream decStream = new CryptoStream(me mStrEnc2,
    tdes.CreateDecr yptor(random, random), CryptoStreamMod e.Read);

    byte[] memString2 = new Byte[memStrEnc2.Leng th];

    decStream.Read( memString2, 0, 16);

    decStream.Close ();



    Thank you for your time

    Josiah



    --
    Josiah Woodhouse


  • Miha Markic

    #2
    Re: decrypting from memory stream

    Hi JW,

    The common cause to your problem is that you probably don't call
    FlushFinalBlock () method before closing CryptoStream when writting data.

    --
    Miha Markic - RightHand .NET consulting & development
    miha at rthand com

    "JW" <josiahwoodhous e@hotmail.com> wrote in message
    news:Ol%23elluq DHA.2636@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    >
    > I can encrypt the contents of a memory stream with no problems, however[/color]
    when[color=blue]
    > decrypting( I'm using the same key an IV ) I get an exception stating bad
    > data.
    >
    > I'm at a lost. I'm actually creating a new memory stream, filling it with
    > the encrypted bytes then trying to read from the crypto stream object. I
    > have used the same approach encrypting to a file handle and then[/color]
    decrypting[color=blue]
    > from the same file and it works.
    >
    >
    >
    > sample: (this same code works with a file stream but not memory stream)[/color]
    what[color=blue]
    > am I missing?
    >
    > MemoryStream memStrEnc2 = new MemoryStream(ne wByte, 0, newByte.Length) ;
    >
    > CryptoStream decStream = new CryptoStream(me mStrEnc2,
    > tdes.CreateDecr yptor(random, random), CryptoStreamMod e.Read);
    >
    > byte[] memString2 = new Byte[memStrEnc2.Leng th];
    >
    > decStream.Read( memString2, 0, 16);
    >
    > decStream.Close ();
    >
    >
    >
    > Thank you for your time
    >
    > Josiah
    >
    >
    >
    > --
    > Josiah Woodhouse
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: decrypting from memory stream

      <"Miha Markic" <miha at rthand com>> wrote:[color=blue]
      > The common cause to your problem is that you probably don't call
      > FlushFinalBlock () method before closing CryptoStream when writting data.[/color]

      That shouldn't make any difference, as I believe CryptoStream.Cl ose
      flushes the final block anyway.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: decrypting from memory stream

        JW <josiahwoodhous e@hotmail.com> wrote:[color=blue]
        > sample: (this same code works with a file stream but not memory stream) what
        > am I missing?
        >
        > MemoryStream memStrEnc2 = new MemoryStream(ne wByte, 0, newByte.Length) ;
        >
        > CryptoStream decStream = new CryptoStream(me mStrEnc2,
        > tdes.CreateDecr yptor(random, random), CryptoStreamMod e.Read);
        >
        > byte[] memString2 = new Byte[memStrEnc2.Leng th];
        >
        > decStream.Read( memString2, 0, 16);
        >
        > decStream.Close ();[/color]

        You're still using Stream.Read the wrong way, as you were before. See
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


        That shouldn't give that kind of exception though. Again, could you
        come up with a short but complete program which demonstrates the
        problem?

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        Working...