要解密的数据长度无效文件加密/解密

时间:2016-02-15 18:10:40

标签: vb.net

我正在尝试为我正在处理的项目创建一个简单的文件加密器/解密器,但是当我尝试解密文件时,我收到以下错误:要解密的数据长度无效(在该行cs.close())

Public Function AES_Encrypt(ByVal bytesToBeEncrypted As Byte(), ByVal passwordBytes as Byte()) As Byte()
    Dim encryptedBytes as Byte() = Nothing
    Dim saltBytes As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8}

    Using ms As New MemoryStream()
        Using AES As New RijndaelManaged
            AES.Keysize = 256
            AES.BlockSize = 128
            Dim key = New Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000)
            AES.Key = key.getBytes(AES.KeySize / 8)
            AES.IV = key.GetBytes(AES.Blocksize / 8)
            AES.Mode = CipherMode.CBC
            Using cs = New CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)
                cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length)
                cs.Close()
            End Using
            encryptedBytes = ms.ToArray
        End Using
    End Using
    Return encryptedBytes
End Function

Public Sub EncryptFile(ByVal file As Stringm ByVal password As String)
    Dim bytesToBeEncrypted As Byte() = System.IO.File.ReadAllBytes(file)
    Dim passwordBytes as Byte() = Encoding.UTF8.GetBytes(password)
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes)
    Dim bytesEncrypted as Byte() = AES_Encrypt(bytesToBeEncrypted, passwordBytes)
    System.IO.File.WriteAllBytes(file, bytesEncrypted)
    System.IO.File.Move(file, file + ".SECURED")
End Sub


Public Function AES_Decrypt(ByVal bytesToBeDecrypted As Byte(), ByVal passwordBytes as Byte()) As Byte()
    Dim decryptedBytes as Byte() = Nothing
    Dim saltBytes As Byte() = New Byte() {1, 2, 3, 4, 5, 6, 7, 8}

    Using ms As New MemoryStream()
        Using AES As New RijndaelManaged
            AES.Keysize = 256
            AES.BlockSize = 128
            Dim key = New Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000)
            AES.Key = key.getBytes(AES.KeySize / 8)
            AES.IV = key.GetBytes(AES.Blocksize / 8)
            AES.Mode = CipherMode.CBC
            Using cs = New CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)
                cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length)
                cs.Close()
            End Using
            decryptedBytes = ms.ToArray
        End Using
    End Using
    Return decryptedBytes
End Function

Public Sub DecryptFile(ByVal file As Stringm ByVal password As String)
    Dim bytesToBeDecrypted As Byte() = System.IO.File.ReadAllBytes(file)
    Dim passwordBytes as Byte() = Encoding.UTF8.GetBytes(password)
    passwordBytes = SHA256.Create().ComputeHash(passwordBytes)

    Dim bytesDecrypted as Byte() = AES_Encrypt(bytesToBeDecrypted, passwordBytes)

    System.IO.File.WriteAllBytes(file, bytesDecrypted)
    Dim extension As String = System.IO.Path.GetExtension(file)
    Dim result As String = file.Substring(0, file.Lenth - extension.Lenth)
    System.IO.File.Move(file, file + ".SECURED")
End Sub

0 个答案:

没有答案