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
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
Comment