How to decrypt a serailized binary formatted object

  • Thread starter DazedAndConfused
  • Start date
D

DazedAndConfused

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 MortgageFileWriter.FileEncrypt

Dim myBuffer As New IO.MemoryStream

Dim OutBuffer As New IO.MemoryStream

Dim fsBuffer As New StreamWriter(OutBuffer)

fsBuffer.Write(company)

fe.EncryptFile(myBuffer, OutBuffer)

bf.Serialize(dataStream, OutBuffer)

dataStream.Close()

Sub EncryptFile( _

ByVal inBuffer As IO.MemoryStream, _

ByVal outBuffer As IO.MemoryStream)

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

'Dim fsOutput As New FileStream(outFileName, FileMode.Create,
FileAccess.Write)

Dim cdk As New PasswordDeriveBytes( _

"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNkFAo3am992z7kgc=", Nothing)



' generate an RC2 key

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

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

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServiceProvider

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.Length - 1)) As Byte

inBuffer.Read(byteBuffer, 0, byteBuffer.Length)

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

'fsInput.Read(byteBuffer, 0, byteBuffer.Length)

' Create CryptoStream with write access to encrypt filestream using RC2

Dim cs As New CryptoStream(outBuffer, rc2.CreateEncryptor(),
CryptoStreamMode.Write)

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

cs.Write(byteBuffer, 0, byteBuffer.Length)

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(inBuffer, FileMode.Open, FileAccess.Read)

'Dim fsOutput As New StreamWriter(outFileName)

Dim cdk As New PasswordDeriveBytes( _

"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNkFAo3am992z7kgc=", Nothing)

' generate an RC2 key

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

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

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServiceProvider

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 cryptostreamDecr As New CryptoStream(inBuffer, rc2.CreateDecryptor(),
CryptoStreamMode.Read)

Dim fsBuffer As New StreamWriter(outBuffer)

' Write out the decrypted file.

fsBuffer.Write(New StreamReader(cryptostreamDecr).ReadToEnd)

fsBuffer.Flush()

'inBuffer.Close()

'fsBuffer.Close()

Catch ex As Exception

Dim str As String

str = ex.Message

End Try





'fsOutput.Write(New StreamReader(cryptostreamDecr).ReadToEnd)

'fsOutput.Flush()

'fsInput.Close()

'fsOutput.Close()

End Sub
 
G

Guest

The following is a simple serialization of the Structure "TestSerialize 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.Serialize(s, t)
' Deserialize
s.Seek(0, SeekOrigin.Begin)
'ot = DirectCast(formatter.Deserialize(s), TestSerialize)
ot = formatter.Deserialize(s)
i = 0
end sub

<Serializable()> Public Structure TestSerialize
Dim i As Integer
Dim a As String
Dim b As Byte()
End Structure
 
D

DazedAndConfused

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.
 
D

DazedAndConfused

I found the answer to my problem at :
Ivan Medvedev's blog
http://blogs.gotdotnet.com/ivanmed/PermaLink.aspx/01380bfa-caf5-40b9-ace6-5973106935a4

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

Although I am still somewhat confused and definitely dazed!

THANK YOU IVAN!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top