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