Can you encrypt a serializable object?

  • Thread starter DazedAndConfused
  • Start date
D

DazedAndConfused

Can you encrpt a serialized object?

Or am I trying to do something that just doesn't work that way?

I am trying to encrypt a serialized object. I can read and write the object
to a file without a problem(IF I don't encrypt it).

The encryption routine I am using works great when I am just reading in text
and writing out encrypted data/Reading in encrpted data and writing out
decrypted text.

BUT when after I encrypt the object, I can not decrypt it, I get a Bad Data
message.

From what I can see the exact same data coming out of the encryption is
going into the decryption. I am stuck.

Call to encrption:

Dim fe As New FileEncrypt

Dim myBuffer As New MemoryStream

Dim OutBuffer As New MemoryStream

Dim bf As New BinaryFormatter

bf.Serialize(myBuffer, company)

myBuffer.Position = 0

fe.EncryptFile(myBuffer, OutBuffer)

Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte

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

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

dataStream.Close()



Encrption:

Sub EncryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

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).ToString()

' 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)

outBuffer.Position = 0

Dim str As Byte() = outBuffer.ToArray

cs.Flush()

inBuffer.Close()

End Sub



Call to Decrption:

Dim fe As New FileEncrypt

Dim myBuffer As New MemoryStream

Dim OutBuffer As New MemoryStream

Dim bf As New BinaryFormatter

bf.Serialize(myBuffer, company)

myBuffer.Position = 0

fe.EncryptFile(myBuffer, OutBuffer)

Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte

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

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

dataStream.Close()



Deryption:

Sub DecryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

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}

Dim byteBuffer(CInt(inBuffer.Length - 1)) As Byte

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

inBuffer.Position = 0

Dim strDataIn As Byte() = inBuffer.ToArray

' 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 BinaryWriter(outBuffer)

' Write out the decrypted file.

fsBuffer.Write(New StreamReader(cryptostreamDecr).ReadToEnd) <======
ex.message ="Bad Data"

outBuffer.Position = 0

fsBuffer.Flush()

Catch ex As Exception

Dim str As String

str = ex.Message

End Try

End Sub
 
D

DazedAndConfused

Yes you can.

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