Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports System.Math
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim j As New saveobject
j.name = "Wally"
j.ID = 145
Dim ms As New MemoryStream
With New BinaryFormatter
.Serialize(ms, j)
End With
Dim s As String = "Qwertyui"
EncryptFile(ms, "C:\TestFile.abc", s)
ms.Flush()
ms.Close()
Dim fs As FileStream = New FileStream("C:\TestFile.abc", FileMode.Open, FileAccess.Read)
Dim ms2 As MemoryStream = DecryptFile(fs, s)
Dim k As saveobject
With New BinaryFormatter
k = .Deserialize(ms2) 'get an error here because ms2 is empty.
End With
Text = k.name
End Sub
Sub EncryptFile(ByVal sInputFile As MemoryStream, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
'Set secret key for DES algorithm.
'A 64-bit key and an IV are required for this provider.
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create the DES encryptor from this instance.
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'Create the crypto stream that transforms the file stream by using DES encryption.
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)
'Read the file text to the byte array.
Dim bytearrayinput(sInputFile.Length - 1) As Byte
sInputFile.Read(bytearrayinput, 0, bytearrayinput.Length)
'Write out the DES encrypted file.
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Flush()
cryptostream.Close()
End Sub
Function DecryptFile(ByVal InputStream As FileStream, _
ByVal sKey As String) As MemoryStream
Dim DES As New DESCryptoServiceProvider()
'A 64-bit key and an IV are required for this provider.
'Set the secret key for the DES algorithm.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set the initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Create the file stream to read the encrypted file back.
' Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'Create the DES decryptor from the DES instance.
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'Create the crypto stream set to read and to do a DES decryption transform on incoming bytes.
Dim cryptostreamDecr As New CryptoStream(InputStream, desdecrypt, CryptoStreamMode.Read)
'Print out the contents of the decrypted file.
Dim OutputMemoryStream As New MemoryStream
Dim fsDecrypted As New StreamWriter(OutputMemoryStream)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
InputStream.Flush()
InputStream.Close()
Return OutputMemoryStream 'Problem - this is empty!
End Function
End Class
<Serializable()> Friend Class saveobject
Friend name As String
Friend ID As Integer
End Class