解密文件夹的内容 - VB.NET

时间:2014-10-25 21:14:42

标签: vb.net encryption

我的任务是制作加密程序,部分任务是加密/解密文件夹。

加密工作正是我想要的,但是当我尝试解密时,它给我一个错误:

  

“要解密的数据长度无效”。

我可以说我真正改变的是我在加密代码中使用DES.CreateDecryptor而不是DES.CreateEncryptor。虽然它看起来比工作更多。

只是想知道我是如何解决这个问题的。我将保留下面代码的相关部分。

Dim folderinfo As New DirectoryInfo(folderpath)

For Each File In Directory.GetFiles(folderinfo.FullName)

    Dim outputFile As String
    outputFile = File

    Dim fsInput As New FileStream(File, FileMode.Open, FileAccess.Read)
    Dim bytearrayinput(fsInput.Length) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    fsInput.Close()

    Dim skey As String
    skey = Encrypt

    Dim fsDecrypted As New FileStream(File, FileMode.Create, FileAccess.Write)
    Dim DES As New DESCryptoServiceProvider
    DES.Key = ASCIIEncoding.ASCII.GetBytes(skey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(skey)

    Dim desdecrypt As ICryptoTransform
    desdecrypt = DES.CreateDecryptor()


    Dim cryptostream As New CryptoStream(fsDecrypted, desdecrypt, CryptoStreamMode.Write)
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)

    cryptostream.Close()
    fsDecrypted.Close()

    txtDecrypt.Text = "All files decrypted"

1 个答案:

答案 0 :(得分:0)

在实例化 array 时,您犯了一个经典错误。应使用(n - 1)的大小来实例化数组,其中 n 是元素的数量。要设置正确的大小,您所要做的就是从fsInput长度中减去1。

Dim bytearrayinput(fsInput.Length - 1) As Byte
相关问题