0

我在序列化對象時使用序列化加密時遇到問題。序列化加密錯誤

這是錯誤:

Failed to deserialize. Reason: End of Stream encountered before parsing was completed

這裏是我的代碼:

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

'這應該是一個由16個ASCII字符組成的字符串?不可以。它應該是一個不可預知且唯一的字節串,最好來自一個安全的隨機數生成器,並且每次調用函數時都應該有所不同。在PBKDF2中使用兩次迭代完全是假的,事實上,在這種情況下完全使用PBKDF2是完全虛假的。 – ntoskrnl

回答

2

加密是有些複雜。首先得到加密工作,只是加密。從一段文字開始:「我像一個犰狳一樣孤獨地徘徊。」使用你的代碼來加密和解密文本,忘記序列化。如果這個工作正常,那麼只有在那時,使用你的工作加密代碼來加密/解密序列化的對象。

你是否成功地序列化/反序列化你的對象沒有任何加密?

匆匆一瞥,您需要將填充設置爲PKCS#7(又名PKCS#5)。您的PaddingMode.None可能是導致此問題的原因。沒有填充您的最終塊可能無法正確處理。顯然,你需要使用相同的填充進行加密和解密。