Jump to content


Encryption Program VB

  • Please log in to reply
1 reply to this topic

#1 Luxx



  • Joined: 01-March 04
  • Location: Birmingham (UK)

Posted 29 October 2013 - 17:05



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.
            FolderPath.Text = FolderBrowserDialog1.SelectedPath

        End If

    End Sub

    Private Sub ListFiles(ByVal folderPath As String)

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

        For Each fileName As String In fileNames

    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)

            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)


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


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

Any guidance would be much appreciated.




#2 +John Teacake

John Teacake


  • Tech Issues Solved: 1
  • Joined: 14-May 03

Posted 29 October 2013 - 20:40

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)