加密/解密文件出错

时间:2010-01-31 19:43:54

标签: vb.net encryption file

当我解密加密文件时;它与原始文件的字节大小不同,文件的哈希值也不同。

我使用File.ReadAllBytes获取文件的字节,并使用密码发送到EncryptBytes。与DecryptBytes一样。

当我收到加密或解密的字节时,我使用File.WriteAllBytes保存它们。

我需要解密文件和原始文件具有相同的散列字节。

请帮忙

这是我的代码:

Public Function EncryptBytes(ByVal pass As String, ByVal bytes() As Byte)

    Dim myRijndael As New RijndaelManaged
    myRijndael.Padding = PaddingMode.Zeros
    myRijndael.KeySize = 256
    myRijndael.BlockSize = 256

    Dim encrypted() As Byte
    Dim key() As Byte = CreateKey(pass)
    Dim IV() As Byte = CreateIV(pass)

    Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

    Dim msEncrypt As New MemoryStream()
    Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

    csEncrypt.Write(bytes, 0, bytes.Length)
    csEncrypt.FlushFinalBlock()

    encrypted = msEncrypt.ToArray()

    Return encrypted

End Function

Public Function DecryptBytes(ByVal pass As String, ByVal bytes() As Byte)

    Dim myRijndael As New RijndaelManaged
    myRijndael.Padding = PaddingMode.Zeros
    myRijndael.KeySize = 256
    myRijndael.BlockSize = 256

    Dim key() As Byte = CreateKey(pass)
    Dim IV() As Byte = CreateIV(pass)

    Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

    Dim fromEncrypt() As Byte = New Byte(bytes.Length) {}

    Dim msDecrypt As New MemoryStream(bytes)
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

    csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

    Return fromEncrypt

End Function

Private Function CreateKey(ByVal strPassword As String) As Byte()
    Dim chrData() As Char = strPassword.ToCharArray

    Dim intLength As Integer = chrData.GetUpperBound(0)

    Dim bytDataToHash(intLength) As Byte

    For i As Integer = 0 To chrData.GetUpperBound(0)
        bytDataToHash(i) = CByte(Asc(chrData(i)))
    Next

    Dim SHA512 As New System.Security.Cryptography.SHA512Managed

    Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)

    Dim bytKey(31) As Byte

    For i As Integer = 0 To 31
        bytKey(i) = bytResult(i)
    Next

    Return bytKey

End Function

Private Function CreateIV(ByVal strPassword As String) As Byte()
    Dim chrData() As Char = strPassword.ToCharArray

    Dim intLength As Integer = chrData.GetUpperBound(0)

    Dim bytDataToHash(intLength) As Byte

    For i As Integer = 0 To chrData.GetUpperBound(0)
        bytDataToHash(i) = CByte(Asc(chrData(i)))
    Next

    Dim SHA512 As New System.Security.Cryptography.SHA512Managed

    Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)

    Dim bytIV(31) As Byte

    For i As Integer = 32 To 47
        bytIV(i - 32) = bytResult(i)
    Next

    Return bytIV

End Function

2 个答案:

答案 0 :(得分:1)

您的DecryptBytes()方法已损坏。您没有使用csDecrypt.Read()的返回值,它告诉您有许多字节被解密。这与fromEncrypt.Length不同。你也很难猜测一个字节数组传递给这个函数的大小。

考虑更改函数以返回MemoryStream。在循环中调用Read()并将读取的内容写入内存流。当Read()返回0时退出循环。

答案 1 :(得分:0)

试试这个:

Public Function EncryptBytes(ByVal pass As String, ByVal bytes() As Byte)

    Dim myRijndael As New RijndaelManaged
    myRijndael.Padding = PaddingMode.PKCS7
    myRijndael.Mode = CipherMode.CBC
    myRijndael.KeySize = 256
    myRijndael.BlockSize = 256

    Dim encrypted() As Byte
    Dim key() As Byte = CreateKey(pass)
    Dim IV() As Byte = CreateIV(pass)

    Dim encryptor As ICryptoTransform = myRijndael.CreateEncryptor(key, IV)

    Dim msEncrypt As New MemoryStream()
    Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

    csEncrypt.Write(bytes, 0, bytes.Length)
    csEncrypt.FlushFinalBlock()

    encrypted = msEncrypt.ToArray()

    Return encrypted

    msEncrypt.Close()
    csEncrypt.Close()

End Function

Public Function DecryptBytes(ByVal pass As String, ByVal bytes() As Byte)

    Dim myRijndael As New RijndaelManaged
    myRijndael.Padding = PaddingMode.PKCS7
    myRijndael.Mode = CipherMode.CBC
    myRijndael.KeySize = 256
    myRijndael.BlockSize = 256

    Dim decrypted() As Byte
    Dim key() As Byte = CreateKey(pass)
    Dim IV() As Byte = CreateIV(pass)

    Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)

    Dim msDecrypt As New MemoryStream()
    Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write)

    csDecrypt.Write(bytes, 0, bytes.Length)
    csDecrypt.FlushFinalBlock()

    decrypted = msDecrypt.ToArray()

    Return decrypted

    msDecrypt.Close()
    csDecrypt.Close()

End Function