我目前正在尝试加密VB.net中的文件夹。为此,我使用In IO.Directory.GetFiles从文件夹中获取每个文件,并使用循环加密每个文件。然后,每个加密文件将以其原始名称保存到某个位置。我目前正在努力了解如何让输出文件以原始名称保存每个文件。到目前为止我的代码显示如下,任何帮助或指导将不胜感激。
Imports System.Windows.Forms
Imports System.Security.Cryptography
Imports System.Security
Imports System.Text
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Folder
Private Sub Select_btn_Click(sender As Object, e As EventArgs) Handles Select_btn.Click
'Allows user to select folder
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
FileSelect_tb.Text = FolderBrowserDialog1.SelectedPath
End If
' Gets files in folder
Dim directory As New IO.DirectoryInfo(FileSelect_tb.Text)
Dim directorylist As IO.FileInfo() = directory.GetFiles()
Dim dra As IO.FileInfo
'Lists files in listbox
For Each dra In directorylist
ListBox1.Items.Add(dra)
Next
End Sub
Private Sub Encrypt_btn_Click(sender As Object, e As EventArgs) Handles Encrypt_btn.Click
'Password authentication
Dim skey As String = InputBox("Enter a password of 8 characters:")
'Check its 8 characters and repeat until its correct
While skey.Length <> 8
skey = InputBox("Password is not correct. Please try again:")
End While
For Each filePath In IO.Directory.GetFiles(FolderBrowserDialog1.SelectedPath)
'Allows user to select file destination
If FolderBrowserDialog2.ShowDialog() = DialogResult.OK Then
Encrypted_tb.Text =
End If
'Reads the file to create encryption
Dim fsInput As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(Encrypted_tb.Text, FileMode.Create, FileAccess.Write)
'Defines encryption method
Dim DES As New DESCryptoServiceProvider
DES.Key = ASCIIEncoding.ASCII.GetBytes(skey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(skey)
'Encrypts File
Dim desencrypt As ICryptoTransform
desencrypt = DES.CreateEncryptor()
Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
Dim bytearrayinput(fsInput.Length) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
fsInput.Close()
fsEncrypted.Close()
Next
End Sub