How to decrypt a serailized binary formatted object

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

    #1

    How to decrypt a serailized binary formatted object

    I encryted a serialized binary formatted object. Now I can't figure out how
    to deserialize it so that I can decrypt it.

    I used this code encrypt and write it out:
    Dim fe As New MortgageFileWri ter.FileEncrypt

    Dim myBuffer As New IO.MemoryStream

    Dim OutBuffer As New IO.MemoryStream

    Dim fsBuffer As New StreamWriter(Ou tBuffer)

    fsBuffer.Write( company)

    fe.EncryptFile( myBuffer, OutBuffer)

    bf.Serialize(da taStream, OutBuffer)

    dataStream.Clos e()

    Sub EncryptFile( _

    ByVal inBuffer As IO.MemoryStream , _

    ByVal outBuffer As IO.MemoryStream )

    'Dim fsInput As New FileStream(inFi leName, FileMode.Open, FileAccess.Read )

    'Dim fsOutput As New FileStream(outF ileName, FileMode.Create ,
    FileAccess.Writ e)

    Dim cdk As New PasswordDeriveB ytes( _

    "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=", Nothing)



    ' generate an RC2 key

    Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

    Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

    ' setup an RC2 object to encrypt with the derived key

    Dim rc2 As New RC2CryptoServic eProvider

    rc2.Key = key

    rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}

    'Read unencrypted file input into the buffer byte array.

    Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte

    inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)

    'Dim byteBuffer(CInt (fsInput.Length ) - 1) As Byte

    'fsInput.Read(b yteBuffer, 0, byteBuffer.Leng th)

    ' Create CryptoStream with write access to encrypt filestream using RC2

    Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
    CryptoStreamMod e.Write)

    ' Write CryptoStream bytes from buffer from offset 0 to end of buffer

    cs.Write(byteBu ffer, 0, byteBuffer.Leng th)

    cs.Flush()

    cs.Close()

    inBuffer.Close( )

    'fsOutput.Close ()

    End Sub



    This is the decription procedure

    Sub DecryptFile( _

    ByVal inBuffer As IO.MemoryStream , _

    ByVal outBuffer As IO.MemoryStream )

    'Create file stream to read encrypted file.

    'Dim fsInput As New FileStream(inBu ffer, FileMode.Open, FileAccess.Read )

    'Dim fsOutput As New StreamWriter(ou tFileName)

    Dim cdk As New PasswordDeriveB ytes( _

    "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=", Nothing)

    ' generate an RC2 key

    Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

    Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

    ' setup an RC2 object to encrypt with the derived key

    Dim rc2 As New RC2CryptoServic eProvider

    rc2.Key = key

    rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}

    ' Create the crypto stream with read access to decrypt incoming bytes using
    RC2.

    Try

    Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
    CryptoStreamMod e.Read)

    Dim fsBuffer As New StreamWriter(ou tBuffer)

    ' Write out the decrypted file.

    fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd)

    fsBuffer.Flush( )

    'inBuffer.Close ()

    'fsBuffer.Close ()

    Catch ex As Exception

    Dim str As String

    str = ex.Message

    End Try





    'fsOutput.Write (New StreamReader(cr yptostreamDecr) .ReadToEnd)

    'fsOutput.Flush ()

    'fsInput.Close( )

    'fsOutput.Close ()

    End Sub


  • DazedAndConfused

    #2
    Re: How to decrypt a serailized binary formatted object

    Never mind, I still do not have the first part right.
    "DazedAndConfus ed" <AceMagoo61@yah oo.com> wrote in message
    news:%23Y%233R2 DyFHA.1252@TK2M SFTNGP09.phx.gb l...[color=blue]
    >I encryted a serialized binary formatted object. Now I can't figure out how
    >to deserialize it so that I can decrypt it.
    >
    > I used this code encrypt and write it out:
    > Dim fe As New MortgageFileWri ter.FileEncrypt
    >
    > Dim myBuffer As New IO.MemoryStream
    >
    > Dim OutBuffer As New IO.MemoryStream
    >
    > Dim fsBuffer As New StreamWriter(Ou tBuffer)
    >
    > fsBuffer.Write( company)
    >
    > fe.EncryptFile( myBuffer, OutBuffer)
    >
    > bf.Serialize(da taStream, OutBuffer)
    >
    > dataStream.Clos e()
    >
    > Sub EncryptFile( _
    >
    > ByVal inBuffer As IO.MemoryStream , _
    >
    > ByVal outBuffer As IO.MemoryStream )
    >
    > 'Dim fsInput As New FileStream(inFi leName, FileMode.Open, FileAccess.Read )
    >
    > 'Dim fsOutput As New FileStream(outF ileName, FileMode.Create ,
    > FileAccess.Writ e)
    >
    > Dim cdk As New PasswordDeriveB ytes( _
    >
    > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
    > Nothing)
    >
    >
    >
    > ' generate an RC2 key
    >
    > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
    >
    > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
    >
    > ' setup an RC2 object to encrypt with the derived key
    >
    > Dim rc2 As New RC2CryptoServic eProvider
    >
    > rc2.Key = key
    >
    > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
    >
    > 'Read unencrypted file input into the buffer byte array.
    >
    > Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte
    >
    > inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)
    >
    > 'Dim byteBuffer(CInt (fsInput.Length ) - 1) As Byte
    >
    > 'fsInput.Read(b yteBuffer, 0, byteBuffer.Leng th)
    >
    > ' Create CryptoStream with write access to encrypt filestream using RC2
    >
    > Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
    > CryptoStreamMod e.Write)
    >
    > ' Write CryptoStream bytes from buffer from offset 0 to end of buffer
    >
    > cs.Write(byteBu ffer, 0, byteBuffer.Leng th)
    >
    > cs.Flush()
    >
    > cs.Close()
    >
    > inBuffer.Close( )
    >
    > 'fsOutput.Close ()
    >
    > End Sub
    >
    >
    >
    > This is the decription procedure
    >
    > Sub DecryptFile( _
    >
    > ByVal inBuffer As IO.MemoryStream , _
    >
    > ByVal outBuffer As IO.MemoryStream )
    >
    > 'Create file stream to read encrypted file.
    >
    > 'Dim fsInput As New FileStream(inBu ffer, FileMode.Open, FileAccess.Read )
    >
    > 'Dim fsOutput As New StreamWriter(ou tFileName)
    >
    > Dim cdk As New PasswordDeriveB ytes( _
    >
    > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
    > Nothing)
    >
    > ' generate an RC2 key
    >
    > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
    >
    > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
    >
    > ' setup an RC2 object to encrypt with the derived key
    >
    > Dim rc2 As New RC2CryptoServic eProvider
    >
    > rc2.Key = key
    >
    > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
    >
    > ' Create the crypto stream with read access to decrypt incoming bytes
    > using RC2.
    >
    > Try
    >
    > Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
    > CryptoStreamMod e.Read)
    >
    > Dim fsBuffer As New StreamWriter(ou tBuffer)
    >
    > ' Write out the decrypted file.
    >
    > fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd)
    >
    > fsBuffer.Flush( )
    >
    > 'inBuffer.Close ()
    >
    > 'fsBuffer.Close ()
    >
    > Catch ex As Exception
    >
    > Dim str As String
    >
    > str = ex.Message
    >
    > End Try
    >
    >
    >
    >
    >
    > 'fsOutput.Write (New StreamReader(cr yptostreamDecr) .ReadToEnd)
    >
    > 'fsOutput.Flush ()
    >
    > 'fsInput.Close( )
    >
    > 'fsOutput.Close ()
    >
    > End Sub
    >
    >[/color]


    Comment

    • Dennis

      #3
      Re: How to decrypt a serailized binary formatted object

      The following is a simple serialization of the Structure "TestSerial ize by
      serializing one instance of the structure "t" then deserializing it back
      into "ot"

      Hope this helps.

      Public Sut TestingSerialze
      Dim t As TestSerialize
      Dim ot As TestSerialize
      Dim formatter As New BinaryFormatter
      Dim s As New MemoryStream
      Dim i As Integer
      t.i = 125
      t.a = "This is a test of Serialilzing a structure"
      t.b = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
      formatter.Seria lize(s, t)
      ' Deserialize
      s.Seek(0, SeekOrigin.Begi n)
      'ot = DirectCast(form atter.Deseriali ze(s), TestSerialize)
      ot = formatter.Deser ialize(s)
      i = 0
      end sub

      <Serializable() > Public Structure TestSerialize
      Dim i As Integer
      Dim a As String
      Dim b As Byte()
      End Structure
      --
      Dennis in Houston


      "DazedAndConfus ed" wrote:
      [color=blue]
      > Never mind, I still do not have the first part right.
      > "DazedAndConfus ed" <AceMagoo61@yah oo.com> wrote in message
      > news:%23Y%233R2 DyFHA.1252@TK2M SFTNGP09.phx.gb l...[color=green]
      > >I encryted a serialized binary formatted object. Now I can't figure out how
      > >to deserialize it so that I can decrypt it.
      > >
      > > I used this code encrypt and write it out:
      > > Dim fe As New MortgageFileWri ter.FileEncrypt
      > >
      > > Dim myBuffer As New IO.MemoryStream
      > >
      > > Dim OutBuffer As New IO.MemoryStream
      > >
      > > Dim fsBuffer As New StreamWriter(Ou tBuffer)
      > >
      > > fsBuffer.Write( company)
      > >
      > > fe.EncryptFile( myBuffer, OutBuffer)
      > >
      > > bf.Serialize(da taStream, OutBuffer)
      > >
      > > dataStream.Clos e()
      > >
      > > Sub EncryptFile( _
      > >
      > > ByVal inBuffer As IO.MemoryStream , _
      > >
      > > ByVal outBuffer As IO.MemoryStream )
      > >
      > > 'Dim fsInput As New FileStream(inFi leName, FileMode.Open, FileAccess.Read )
      > >
      > > 'Dim fsOutput As New FileStream(outF ileName, FileMode.Create ,
      > > FileAccess.Writ e)
      > >
      > > Dim cdk As New PasswordDeriveB ytes( _
      > >
      > > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
      > > Nothing)
      > >
      > >
      > >
      > > ' generate an RC2 key
      > >
      > > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
      > >
      > > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
      > >
      > > ' setup an RC2 object to encrypt with the derived key
      > >
      > > Dim rc2 As New RC2CryptoServic eProvider
      > >
      > > rc2.Key = key
      > >
      > > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
      > >
      > > 'Read unencrypted file input into the buffer byte array.
      > >
      > > Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte
      > >
      > > inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)
      > >
      > > 'Dim byteBuffer(CInt (fsInput.Length ) - 1) As Byte
      > >
      > > 'fsInput.Read(b yteBuffer, 0, byteBuffer.Leng th)
      > >
      > > ' Create CryptoStream with write access to encrypt filestream using RC2
      > >
      > > Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
      > > CryptoStreamMod e.Write)
      > >
      > > ' Write CryptoStream bytes from buffer from offset 0 to end of buffer
      > >
      > > cs.Write(byteBu ffer, 0, byteBuffer.Leng th)
      > >
      > > cs.Flush()
      > >
      > > cs.Close()
      > >
      > > inBuffer.Close( )
      > >
      > > 'fsOutput.Close ()
      > >
      > > End Sub
      > >
      > >
      > >
      > > This is the decription procedure
      > >
      > > Sub DecryptFile( _
      > >
      > > ByVal inBuffer As IO.MemoryStream , _
      > >
      > > ByVal outBuffer As IO.MemoryStream )
      > >
      > > 'Create file stream to read encrypted file.
      > >
      > > 'Dim fsInput As New FileStream(inBu ffer, FileMode.Open, FileAccess.Read )
      > >
      > > 'Dim fsOutput As New StreamWriter(ou tFileName)
      > >
      > > Dim cdk As New PasswordDeriveB ytes( _
      > >
      > > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
      > > Nothing)
      > >
      > > ' generate an RC2 key
      > >
      > > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
      > >
      > > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
      > >
      > > ' setup an RC2 object to encrypt with the derived key
      > >
      > > Dim rc2 As New RC2CryptoServic eProvider
      > >
      > > rc2.Key = key
      > >
      > > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
      > >
      > > ' Create the crypto stream with read access to decrypt incoming bytes
      > > using RC2.
      > >
      > > Try
      > >
      > > Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
      > > CryptoStreamMod e.Read)
      > >
      > > Dim fsBuffer As New StreamWriter(ou tBuffer)
      > >
      > > ' Write out the decrypted file.
      > >
      > > fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd)
      > >
      > > fsBuffer.Flush( )
      > >
      > > 'inBuffer.Close ()
      > >
      > > 'fsBuffer.Close ()
      > >
      > > Catch ex As Exception
      > >
      > > Dim str As String
      > >
      > > str = ex.Message
      > >
      > > End Try
      > >
      > >
      > >
      > >
      > >
      > > 'fsOutput.Write (New StreamReader(cr yptostreamDecr) .ReadToEnd)
      > >
      > > 'fsOutput.Flush ()
      > >
      > > 'fsInput.Close( )
      > >
      > > 'fsOutput.Close ()
      > >
      > > End Sub
      > >
      > >[/color]
      >
      >
      >[/color]

      Comment

      • DazedAndConfused

        #4
        Re: How to decrypt a serailized binary formatted object

        I'll take a closer look, but the serialization has been working, the problem
        arises when I attempt to encrypt the object and then try to decrpt it. I'm
        not even sure you can do this.
        "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
        news:950017E6-642B-4848-BC23-88CFF8A24182@mi crosoft.com...[color=blue]
        > The following is a simple serialization of the Structure "TestSerial ize by
        > serializing one instance of the structure "t" then deserializing it back
        > into "ot"
        >
        > Hope this helps.
        >
        > Public Sut TestingSerialze
        > Dim t As TestSerialize
        > Dim ot As TestSerialize
        > Dim formatter As New BinaryFormatter
        > Dim s As New MemoryStream
        > Dim i As Integer
        > t.i = 125
        > t.a = "This is a test of Serialilzing a structure"
        > t.b = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
        > 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
        > formatter.Seria lize(s, t)
        > ' Deserialize
        > s.Seek(0, SeekOrigin.Begi n)
        > 'ot = DirectCast(form atter.Deseriali ze(s), TestSerialize)
        > ot = formatter.Deser ialize(s)
        > i = 0
        > end sub
        >
        > <Serializable() > Public Structure TestSerialize
        > Dim i As Integer
        > Dim a As String
        > Dim b As Byte()
        > End Structure
        > --
        > Dennis in Houston
        >
        >
        > "DazedAndConfus ed" wrote:
        >[color=green]
        >> Never mind, I still do not have the first part right.
        >> "DazedAndConfus ed" <AceMagoo61@yah oo.com> wrote in message
        >> news:%23Y%233R2 DyFHA.1252@TK2M SFTNGP09.phx.gb l...[color=darkred]
        >> >I encryted a serialized binary formatted object. Now I can't figure out
        >> >how
        >> >to deserialize it so that I can decrypt it.
        >> >
        >> > I used this code encrypt and write it out:
        >> > Dim fe As New MortgageFileWri ter.FileEncrypt
        >> >
        >> > Dim myBuffer As New IO.MemoryStream
        >> >
        >> > Dim OutBuffer As New IO.MemoryStream
        >> >
        >> > Dim fsBuffer As New StreamWriter(Ou tBuffer)
        >> >
        >> > fsBuffer.Write( company)
        >> >
        >> > fe.EncryptFile( myBuffer, OutBuffer)
        >> >
        >> > bf.Serialize(da taStream, OutBuffer)
        >> >
        >> > dataStream.Clos e()
        >> >
        >> > Sub EncryptFile( _
        >> >
        >> > ByVal inBuffer As IO.MemoryStream , _
        >> >
        >> > ByVal outBuffer As IO.MemoryStream )
        >> >
        >> > 'Dim fsInput As New FileStream(inFi leName, FileMode.Open,
        >> > FileAccess.Read )
        >> >
        >> > 'Dim fsOutput As New FileStream(outF ileName, FileMode.Create ,
        >> > FileAccess.Writ e)
        >> >
        >> > Dim cdk As New PasswordDeriveB ytes( _
        >> >
        >> > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
        >> > Nothing)
        >> >
        >> >
        >> >
        >> > ' generate an RC2 key
        >> >
        >> > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
        >> >
        >> > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
        >> >
        >> > ' setup an RC2 object to encrypt with the derived key
        >> >
        >> > Dim rc2 As New RC2CryptoServic eProvider
        >> >
        >> > rc2.Key = key
        >> >
        >> > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
        >> >
        >> > 'Read unencrypted file input into the buffer byte array.
        >> >
        >> > Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte
        >> >
        >> > inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)
        >> >
        >> > 'Dim byteBuffer(CInt (fsInput.Length ) - 1) As Byte
        >> >
        >> > 'fsInput.Read(b yteBuffer, 0, byteBuffer.Leng th)
        >> >
        >> > ' Create CryptoStream with write access to encrypt filestream using RC2
        >> >
        >> > Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
        >> > CryptoStreamMod e.Write)
        >> >
        >> > ' Write CryptoStream bytes from buffer from offset 0 to end of buffer
        >> >
        >> > cs.Write(byteBu ffer, 0, byteBuffer.Leng th)
        >> >
        >> > cs.Flush()
        >> >
        >> > cs.Close()
        >> >
        >> > inBuffer.Close( )
        >> >
        >> > 'fsOutput.Close ()
        >> >
        >> > End Sub
        >> >
        >> >
        >> >
        >> > This is the decription procedure
        >> >
        >> > Sub DecryptFile( _
        >> >
        >> > ByVal inBuffer As IO.MemoryStream , _
        >> >
        >> > ByVal outBuffer As IO.MemoryStream )
        >> >
        >> > 'Create file stream to read encrypted file.
        >> >
        >> > 'Dim fsInput As New FileStream(inBu ffer, FileMode.Open,
        >> > FileAccess.Read )
        >> >
        >> > 'Dim fsOutput As New StreamWriter(ou tFileName)
        >> >
        >> > Dim cdk As New PasswordDeriveB ytes( _
        >> >
        >> > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
        >> > Nothing)
        >> >
        >> > ' generate an RC2 key
        >> >
        >> > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
        >> >
        >> > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
        >> >
        >> > ' setup an RC2 object to encrypt with the derived key
        >> >
        >> > Dim rc2 As New RC2CryptoServic eProvider
        >> >
        >> > rc2.Key = key
        >> >
        >> > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
        >> >
        >> > ' Create the crypto stream with read access to decrypt incoming bytes
        >> > using RC2.
        >> >
        >> > Try
        >> >
        >> > Dim cryptostreamDec r As New CryptoStream(in Buffer,
        >> > rc2.CreateDecry ptor(),
        >> > CryptoStreamMod e.Read)
        >> >
        >> > Dim fsBuffer As New StreamWriter(ou tBuffer)
        >> >
        >> > ' Write out the decrypted file.
        >> >
        >> > fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd)
        >> >
        >> > fsBuffer.Flush( )
        >> >
        >> > 'inBuffer.Close ()
        >> >
        >> > 'fsBuffer.Close ()
        >> >
        >> > Catch ex As Exception
        >> >
        >> > Dim str As String
        >> >
        >> > str = ex.Message
        >> >
        >> > End Try
        >> >
        >> >
        >> >
        >> >
        >> >
        >> > 'fsOutput.Write (New StreamReader(cr yptostreamDecr) .ReadToEnd)
        >> >
        >> > 'fsOutput.Flush ()
        >> >
        >> > 'fsInput.Close( )
        >> >
        >> > 'fsOutput.Close ()
        >> >
        >> > End Sub
        >> >
        >> >[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • DazedAndConfused

          #5
          Re: How to decrypt a serailized binary formatted object

          I found the answer to my problem at :
          Ivan Medvedev's blog


          After banging my head for five days the problem was using
          CryptoStreamMod e.Read in the Decryption. You can use it
          (CryptoStreamMo de.Read ) for decrypting file and network streams, but
          apparently you need to use CryptoStreamMod e.Write for Memory.

          Although I am still somewhat confused and definitely dazed!

          THANK YOU IVAN!!


          "DazedAndConfus ed" <AceMagoo61@yah oo.com> wrote in message
          news:%23Y%233R2 DyFHA.1252@TK2M SFTNGP09.phx.gb l...[color=blue]
          >I encryted a serialized binary formatted object. Now I can't figure out how
          >to deserialize it so that I can decrypt it.
          >
          > I used this code encrypt and write it out:
          > Dim fe As New MortgageFileWri ter.FileEncrypt
          >
          > Dim myBuffer As New IO.MemoryStream
          >
          > Dim OutBuffer As New IO.MemoryStream
          >
          > Dim fsBuffer As New StreamWriter(Ou tBuffer)
          >
          > fsBuffer.Write( company)
          >
          > fe.EncryptFile( myBuffer, OutBuffer)
          >
          > bf.Serialize(da taStream, OutBuffer)
          >
          > dataStream.Clos e()
          >
          > Sub EncryptFile( _
          >
          > ByVal inBuffer As IO.MemoryStream , _
          >
          > ByVal outBuffer As IO.MemoryStream )
          >
          > 'Dim fsInput As New FileStream(inFi leName, FileMode.Open, FileAccess.Read )
          >
          > 'Dim fsOutput As New FileStream(outF ileName, FileMode.Create ,
          > FileAccess.Writ e)
          >
          > Dim cdk As New PasswordDeriveB ytes( _
          >
          > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
          > Nothing)
          >
          >
          >
          > ' generate an RC2 key
          >
          > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
          >
          > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
          >
          > ' setup an RC2 object to encrypt with the derived key
          >
          > Dim rc2 As New RC2CryptoServic eProvider
          >
          > rc2.Key = key
          >
          > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
          >
          > 'Read unencrypted file input into the buffer byte array.
          >
          > Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte
          >
          > inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)
          >
          > 'Dim byteBuffer(CInt (fsInput.Length ) - 1) As Byte
          >
          > 'fsInput.Read(b yteBuffer, 0, byteBuffer.Leng th)
          >
          > ' Create CryptoStream with write access to encrypt filestream using RC2
          >
          > Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
          > CryptoStreamMod e.Write)
          >
          > ' Write CryptoStream bytes from buffer from offset 0 to end of buffer
          >
          > cs.Write(byteBu ffer, 0, byteBuffer.Leng th)
          >
          > cs.Flush()
          >
          > cs.Close()
          >
          > inBuffer.Close( )
          >
          > 'fsOutput.Close ()
          >
          > End Sub
          >
          >
          >
          > This is the decription procedure
          >
          > Sub DecryptFile( _
          >
          > ByVal inBuffer As IO.MemoryStream , _
          >
          > ByVal outBuffer As IO.MemoryStream )
          >
          > 'Create file stream to read encrypted file.
          >
          > 'Dim fsInput As New FileStream(inBu ffer, FileMode.Open, FileAccess.Read )
          >
          > 'Dim fsOutput As New StreamWriter(ou tFileName)
          >
          > Dim cdk As New PasswordDeriveB ytes( _
          >
          > "1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
          > Nothing)
          >
          > ' generate an RC2 key
          >
          > Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
          >
          > Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)
          >
          > ' setup an RC2 object to encrypt with the derived key
          >
          > Dim rc2 As New RC2CryptoServic eProvider
          >
          > rc2.Key = key
          >
          > rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
          >
          > ' Create the crypto stream with read access to decrypt incoming bytes
          > using RC2.
          >
          > Try
          >
          > Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
          > CryptoStreamMod e.Read)
          >
          > Dim fsBuffer As New StreamWriter(ou tBuffer)
          >
          > ' Write out the decrypted file.
          >
          > fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd)
          >
          > fsBuffer.Flush( )
          >
          > 'inBuffer.Close ()
          >
          > 'fsBuffer.Close ()
          >
          > Catch ex As Exception
          >
          > Dim str As String
          >
          > str = ex.Message
          >
          > End Try
          >
          >
          >
          >
          >
          > 'fsOutput.Write (New StreamReader(cr yptostreamDecr) .ReadToEnd)
          >
          > 'fsOutput.Flush ()
          >
          > 'fsInput.Close( )
          >
          > 'fsOutput.Close ()
          >
          > End Sub
          >
          >[/color]


          Comment

          Working...