Deserializaing an Object
Deserializaing an Object
Deserializaing an Object
Deserializaing an Object
Deserializaing an Object
Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object
Deserializaing an Object Deserializaing an Object
Deserializaing an Object
Go Back  Xtreme Visual Basic Talk > > > Deserializaing an Object


Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2013, 02:42 AM
Canning_s Canning_s is offline
Newcomer
 
Join Date: Jul 2013
Posts: 1
Default Deserializaing an Object


I am having trouble deserializing an object. This is the error:

Quote:
Failed to deserialize. Reason: End of Stream encountered before parsing was completed
Here is my code:

Code:
Imports System.IO
Imports System.Security.Cryptography
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Text

Module TestModEncryption

    Public Sub SaveEncryptedObjectToFile(FileName As String, Item As Object)
        Dim fs As FileStream
        Dim encryptor As CryptoStream

        Dim formatter As New BinaryFormatter

        Dim password As String = "MyPassword"
        Dim salt As String = "InitialVector123"

        Dim AES As AesManaged = New AesManaged
        AES.Padding = PaddingMode.None
        AES.Mode = CipherMode.CBC

        Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
        Dim PasswordIterations As Integer = 2
        Dim InitialVector As String = "InitialVector123" 'This should be a string of 16 ASCII characters.
        Dim KeySize As Integer = 256 'Can be 128, 192, or 256.

        Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
        Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
        Dim DerivedPassword As New Rfc2898DeriveBytes(password, SaltValueBytes, PasswordIterations)
        Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8))

        Dim encryptTransf As ICryptoTransform = AES.CreateEncryptor(KeyBytes, InitialVectorBytes)

        fs = New FileStream(FileName, FileMode.Create)
        encryptor = New CryptoStream(fs, encryptTransf, CryptoStreamMode.Write)

        Try
            formatter.Serialize(encryptor, Item)
        Catch e As SerializationException
            Console.WriteLine("Failed to serialize. Reason: " & e.Message)
            Throw
        Finally
            fs.Close()
        End Try
    End Sub

    Public Function OpenEncryptedObjectFromFile(FileName As String) As Object
        Dim fs As New FileStream(FileName, FileMode.Open)
        Dim decryptor As CryptoStream

        Dim ItemToReturn As New Object

        Dim password As String = "MyPassword"
        Dim salt As String = "InitialVector123"

        Dim AES As AesManaged = New AesManaged
        AES.Padding = PaddingMode.None
        AES.Mode = CipherMode.CBC

        Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
        Dim PasswordIterations As Integer = 2
        Dim InitialVector As String = "InitialVector123" 'This should be a string of 16 ASCII characters.
        Dim KeySize As Integer = 256 'Can be 128, 192, or 256.

        Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
        Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(salt)
        Dim DerivedPassword As New Rfc2898DeriveBytes(password, SaltValueBytes, PasswordIterations)
        Dim KeyBytes As Byte() = DerivedPassword.GetBytes(CInt(KeySize / 8))

        Dim decryptTrans As ICryptoTransform = AES.CreateDecryptor(KeyBytes, InitialVectorBytes)

        Try
            Dim formatter As New BinaryFormatter

            decryptor = New CryptoStream(fs, decryptTrans, CryptoStreamMode.Read)
            ItemToReturn = DirectCast(formatter.Deserialize(decryptor), Object)
            Return ItemToReturn
        Catch e As SerializationException
            MsgBox("Failed to deserialize. Reason: " & e.Message)
            Return Nothing
            'Throw
        Finally
            fs.Close()
        End Try
    End Function

End Module
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Deserializaing an Object
Deserializaing an Object
Deserializaing an Object Deserializaing an Object
Deserializaing an Object
Deserializaing an Object
Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object Deserializaing an Object
Deserializaing an Object
Deserializaing an Object
 
Deserializaing an Object
Deserializaing an Object
 
-->