Hi All,

I have written my-self a file encryption application, but I am getting the following error from on this peice of code

the eroor is : NAME 'it' is not declared

Please see my code below

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
    Inherits System.Windows.Forms.Form
    'creates an 8 bit long array to hold the key
    Public TheKey(7) As Byte
    'some random values into the vector
    Private Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}

    'Encryption Procedure

    Sub Encrypt(ByVal inName As String, ByVal outName As String)
        Try
            Dim storage(4096) As Byte 'create buffer
            Dim totalBytesWritten As Long = 8 'keeps track of bytes written
            Dim packageSize As Integer 'specifies the number of bytes written at one time
            'declair the file streams
            Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
            Dim fout As New FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write)
            fout.SetLength(0)
            Dim totalFileLength As Long = fin.Length 'specefies the size of the source file
            'create the crypto object
            Dim des As New DESCryptoServiceProvider()
            Dim crStream As New CryptoStream(fout, des.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write)
            'flow the streams

            While totalBytesWritten < totalFileLength
                packageSize = fin.Read(storage, 0, 4096)
                crStream.Write(storage, 0, packageSize)
                totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
            End While
            crStream.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    'Decryption Procedure

    Sub Decrypt(ByVal inName As String, ByVal outName As String)
        Try
            Dim storage(4096) As Byte
            Dim totalBytesWritten As Long = 8
            Dim packageSize As Integer
            Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
            Dim fout As New FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write)
            fout.SetLength(0)
            Dim totalFileLength As Long = fin.Length
            Dim des As New DESCryptoServiceProvider()
            Dim crStream As New CryptoStream(fout, des.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write)
            Dim ex As Exception

            While totalBytesWritten < totalFileLength
                packageSize = fin.Read(storage, 0, 4096)
                crStream.Write(storage, 0, packageSize)
                totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
                Console.WriteLine("Processed {0} bytes, {1} bytes total", packageSize, totalBytesWritten)

            End While
            crStream.Close()
        Catch ex As Exception
            MsgBox(ex.Message & "PLEASE CHECK YOUR PASSWORD")
        End Try
    End Sub


    Private Sub btnLoadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadFile.Click
        OpenFileDialog1.ShowDialog()
        txtBoxFileLocation.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub btnEncryptorDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncryptorDecrypt.Click
        Dim targetFile, sourcefile As String
        Dim ext As String 'file extention
        Dim mainPath As String 'the file path minus the extension
        Dim n As Integer 'location of the period in filepath
        'check to see if user has entered a filename at all

        If txtBoxFileLocation.Text = "" Or txtBoxPassword.Text = "" Then
            MsgBox("PLEASE ENTER A FILENAME AND A PASSWORD") : Exit Sub

            n = txtBoxFileLocation.Text.IndexOf(".") 'returns -1 if there is no extension

            If n <> -1 Then
                ext = txtBoxFileLocation.Text.Substring(n + 1)
                mainPath = txtBoxFileLocation.Text.Substring(0, txtBoxFileLocation.Text.Length - ext.Length - 1)
            Else
                mainPath = txtBoxFileLocation.Text
            End If
        End If

        If mainPath.Substring(mainPath.Length - 2) = "xx" Then
            'decrypt
            sourcefile = txtBoxFileLocation.Text
            'compose filepath by removing the "xx":
            mainPath = mainPath.Substring(0, mainPath.Length - 2)

            If ext <> "" Then
                mainPath &= "." & ext
                targetFile = mainPath

                CreateKey(txtBoxPassword.Text)
                Decrypt(sourcefile, targetFile)
                lblResult.Text = "DECRYPTION COMPLETE"
                Exit Sub
            End If

        End If

        'Encrypt
        sourcefile = txtBoxFileLocation.Text
        'compose the ecrypted files filepath by appending "xx":
        mainPath &= "xx"

        If ext <> "" Then
            mainPath &= "." & ext
            targetFile = mainPath
            Createkey(txtBoxPassword.Text) 'create the key
            Encrypt(sourcefile, targetFile)
            lblResult.Text = "ECRYPTION COMPLETE"
        End If

    End Sub


    Sub CreateKey(ByVal strKey As String)
        Dim arrByte(7) As Byte
        Dim AscEncod As New ASCIIEncoding()
        Dim i As Integer = 0
        AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)
        Dim hashSha As New SHA1CryptoServiceProvider()
        Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)

        For i = 0 To 7
            TheKey(i) = arrHash(i)
        Next i

    End Sub

End Class

The program is simple enough, I use the load file button and choose the file I want to encrypt then type in a eight digit password and click the encrypt button.

I really just don't understand the error.

Can anyone help.

Thanks

John

Edited 3 Years Ago by pyTony: fixed formating

&lt and &gt is normally used in HTML to replace < and >but not in actual code. Replace those &lt and &gt with < and > in your code and it should work.

&lt and &gt is normally used in HTML to replace < and >but not in actual code. Replace those &lt and &gt with < and > in your code and it should work.

Hi Hericles,

Thanks for the reply, that has sorted that issue, I had no idea that &alt translated to <

I have attempted to run the software and I get a Null Exception on this line of the code

If mainPath.Substring(mainPath.Length - 2) = "xx" Then

I can't see why that would happen as all my controls are coded in??

I am going to create seperate buttons for the ecrypt and decrypt process and see if that helps.

What are you trying to compare to "xx", are you sure you don't mean

If mainPath.Substring(0, 2) = "xx" Then

Hi Chris,

This line of code checks to see if the file has "xx" in the file name, if it does then it is an encrypted file and it wants decrypting.

If it does not have "xx" then it will be encrypted.

The variable mainPath is a global one and is written

Dim mainpath As String

I have tried writing If mainPath.Substring(0, 2) = txtBoxFileLocation.Text = "xx" Then... but I still get the same exception.

This project is s close to being completed I can almost taste it, I just need to clear this issue. (I hope it's the last issue lol)

Edited 4 Years Ago by discovery-power: n/a

try

if instr(mainPath, "xx") > 0 then msgbox "mainPath containsxx"

This seems to throw the msgbox up even though xx is not in the file name.

I think I might have to re-thing the way I have written out the code altogether, I am not really comfortable with it, the way it's laid out doesn't seem to make much sence, it's all just willy nilly.

Back to the drowing board with this one

Thanks for the help.

John

This article has been dead for over six months. Start a new discussion instead.