Sign in to follow this  

Encryption Program VB

Recommended Posts

Luxx    0



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.




Share this post

Link to post
Share on other sites
+John Teacake    446

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)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.