在VB.NET中加密和解密字节数组的问题

时间:2014-07-30 15:54:07

标签: vb.net encryption

我在.NET中加密和解密字节数组时遇到了困难。我很感激帮助理解为什么它目前没有工作。

以下是代码:

Public Shared Function GenerateKey(password As String, Size As Int32) As Byte()
    Dim rfc As New Rfc2898DeriveBytes(password, Salt, iterations:=973)
    Return rfc.GetBytes(Size)
End Function

Public Shared Function EncryptArray(ByRef data As Byte(), password As String) As Byte()
    Dim key() As Byte = GenerateKey(password, 8)
    Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
    Using cp As New DESCryptoServiceProvider
        Using ms As New MemoryStream
            Dim bf As New BinaryFormatter
            Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
                bf.Serialize(cs, data)
                cs.FlushFinalBlock()
                Return ms.GetBuffer
            End Using
        End Using
    End Using
End Function

Public Shared Sub DecryptArray(ByRef data As Byte(), password As String)
    Dim key() As Byte = GenerateKey(password, 8)
    Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
    Using cp As New DESCryptoServiceProvider
        Using ms As New MemoryStream(data)                 
            Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Read)
                Using br As New BinaryReader(cs)
                    Dim bf As New BinaryFormatter                          
                    data = DirectCast(bf.Deserialize(cs), Byte())
                End Using
            End Using
        End Using
    End Using
End Sub

我的通话程序:

Public Sub TestArrayEncryption()
    Dim text As String = "  imkkj r As ing = Hash.gHa.ing(s, ""sh2"", ""DGF&^***YHGJ&^*&(KI~@"")"""
    Dim pw As String = "pasword12345678901234567890"
    Dim arr As Byte() = Encryption.EncryptArray(Encryption.ConvertUTF8ToByteArray(text), pw)

    Encryption.DecryptArray(arr, pw)
    Dim txt2 As String = Encryption.ConvertByteArrayToUTF8(arr)
    Assert.AreEqual(txt2, text)
End Sub

此行data = DirectCast(bf.Deserialize(ms), Byte())上的代码失败并显示加密异常'错误数据'。

增加:

我设法使用此网站上的其他代码进行此操作。我注意到在使用这些新例程时,加密数组比输入数组大。在我的原始代码中并非如此。我仍然有兴趣知道为什么原始代码不起作用。

工作代码:

        Public Shared Function DecryptArray2(ByRef data As Byte(), password As String) As Byte()
        Dim key() As Byte = GenerateKey(password, 8)
        Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
        Dim ddata As Byte()
        Using ms As New MemoryStream
            Using cp As New DESCryptoServiceProvider
                Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Write)
                    cs.Write(data, 0, data.Length)
                    cs.Close()
                    ddata = ms.ToArray
                End Using
            End Using
        End Using
        Return ddata
    End Function

    Public Shared Function EncryptArray2(ByRef data As Byte(), password As String) As Byte()
        Dim key() As Byte = GenerateKey(password, 8)
        Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
        Dim edata As Byte()
        Using ms As New MemoryStream
            Using cp As New DESCryptoServiceProvider
                Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
                    cs.Write(data, 0, data.Length)
                    cs.Close()
                    edata = ms.ToArray
                End Using
            End Using
        End Using
        Return edata
    End Function

1 个答案:

答案 0 :(得分:2)

您不应该序列化CryptoStream。只需将数据写入其中并从底层流中获取数组。加密:

    Using ms As New MemoryStream
        Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(data, 0, data.Length)
            cs.FlushFinalBlock()
            Return ms.ToArray()
        End Using
    End Using

和解密:

    Using ms As New MemoryStream(data)                 
        Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Read)
            Using ms2 As New MemoryStream()
                cs.CopyTo(ms2) 
                data = ms2.ToArray()
            End Using
        End Using
    End Using