Vb.net 要解密的数据长度无效|文件加密/解密

Vb.net 要解密的数据长度无效|文件加密/解密,vb.net,Vb.net,我正在尝试为我正在处理的项目制作一个简单的文件加密/解密程序,但是当我尝试解密一个文件时,我得到了以下错误:要解密的数据长度无效(在cs.close()行) 你能发布堆栈跟踪吗?另外,如果您查看using语句的位置:cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Lenth)您有一个类型-o,它应该是cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Length)您错过了长度为g的字符。。。这个文件

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


你能发布堆栈跟踪吗?另外,如果您查看using语句的位置:
cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Lenth)
您有一个类型-o,它应该是
cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Length)
您错过了长度为g的字符。。。这个文件似乎永远不会被创建…你能发布堆栈跟踪吗?另外,如果您查看using语句的位置:
cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Lenth)
您有一个类型-o,它应该是
cs.Write(bytesToBeDecrypted,0,bytesToBeDecrypted.Length)
您错过了长度为g的字符。。。似乎永远不会创建此文件。。。
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