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