• 0

Encryption Program VB


Question

Hello

 

I've been assigned a task of creating an encryption program to encrypt text file, images and folder directorys using visual basic.

 

I've worked out a way to encrypt / decrpyt any file using DESCryptoServiceProvider and now have a working loop that will work through a folder directory encrpyting and decrpyting all files within the folder.

 

when you select a folder i have a Checked list box that when using the following code will display the list of files inside.

Private Sub SelectFolder_Click(sender As System.Object, e As System.EventArgs) Handles SelectFolder.Click
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            ' List files in the folder.
            ListFiles(FolderBrowserDialog1.SelectedPath)
            FolderPath.Text = FolderBrowserDialog1.SelectedPath

        End If

    End Sub

    Private Sub ListFiles(ByVal folderPath As String)
        FileList.Items.Clear()

        Dim fileNames = My.Computer.FileSystem.GetFiles(
            folderPath, FileIO.SearchOption.SearchTopLevelOnly)

        For Each fileName As String In fileNames
            FileList.Items.Add(fileName)
        Next

    End Sub

I want to be able to only encrypt the files if they have a checked box state of checked but i'm struggling to figure this out.

 

the code for my loop to encrpyt all files within a folder is below:


    Private Sub EncryptDirRecursive(ByVal folderpath As String)


        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


        'check if this dir exists
        Dim vDirInfo As New DirectoryInfo(folderpath)
        If Not vDirInfo.Exists Then Exit Sub

        'get all files' sizes in current path
        On Error Resume Next

        For Each fileName As String In Directory.GetFiles(vDirInfo.FullName)

            'do something (such as encrypt)
            Dim outputfile As String


            outputfile = fileName

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

            Dim fsEncrypted As New FileStream(fileName, FileMode.Create, FileAccess.Write)
            Dim DES As New DESCryptoServiceProvider
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)

            Dim desencrypt As ICryptoTransform
            desencrypt = DES.CreateEncryptor()


            Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)

            cryptostream.Close()
            fsEncrypted.Close()

            MessageBox.Show("Encryption Complete:" & fileName)


        Next

        'do the same for all subfolders
        For Each vSubDir As String In Directory.GetDirectories(vDirInfo.FullName)
            EncryptDirRecursive(vSubDir)
        Next
    End Sub

Any guidance would be much appreciated.

 

Thanks

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

The way I would do this is read in the list of files in said directory from the standard Windows API, Add your own checkbox and have some sort of array (FileName/Path, Boolean value to say ShouldEncrypt)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.