Code:
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Public Class frmMain
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents Label8 As System.Windows.Forms.Label
Friend WithEvents txtDestinationEncrypt As System.Windows.Forms.TextBox
Friend WithEvents btnEncrypt As System.Windows.Forms.Button
Friend WithEvents txtConPassEncrypt As System.Windows.Forms.TextBox
Friend WithEvents txtPassEncrypt As System.Windows.Forms.TextBox
Friend WithEvents txtFileToEncrypt As System.Windows.Forms.TextBox
Friend WithEvents pbStatus As System.Windows.Forms.ProgressBar
Friend WithEvents txtDestinationDecrypt As System.Windows.Forms.TextBox
Friend WithEvents btnDecrypt As System.Windows.Forms.Button
Friend WithEvents txtConPassDecrypt As System.Windows.Forms.TextBox
Friend WithEvents txtPassDecrypt As System.Windows.Forms.TextBox
Friend WithEvents txtFileToDecrypt As System.Windows.Forms.TextBox
Friend WithEvents btnChangeEncrypt As System.Windows.Forms.Button
Friend WithEvents btnBrowseEncrypt As System.Windows.Forms.Button
Friend WithEvents btnChangeDecrypt As System.Windows.Forms.Button
Friend WithEvents btnBrowseDecrypt As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog As System.Windows.Forms.OpenFileDialog
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents FolderBrowserDialog As System.Windows.Forms.FolderBrowserDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TabControl1 = New System.Windows.Forms.TabControl
Me.TabPage1 = New System.Windows.Forms.TabPage
Me.btnChangeEncrypt = New System.Windows.Forms.Button
Me.txtDestinationEncrypt = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.btnEncrypt = New System.Windows.Forms.Button
Me.txtConPassEncrypt = New System.Windows.Forms.TextBox
Me.txtPassEncrypt = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.btnBrowseEncrypt = New System.Windows.Forms.Button
Me.txtFileToEncrypt = New System.Windows.Forms.TextBox
Me.TabPage2 = New System.Windows.Forms.TabPage
Me.btnChangeDecrypt = New System.Windows.Forms.Button
Me.txtDestinationDecrypt = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.btnDecrypt = New System.Windows.Forms.Button
Me.txtConPassDecrypt = New System.Windows.Forms.TextBox
Me.txtPassDecrypt = New System.Windows.Forms.TextBox
Me.Label6 = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.Label8 = New System.Windows.Forms.Label
Me.btnBrowseDecrypt = New System.Windows.Forms.Button
Me.txtFileToDecrypt = New System.Windows.Forms.TextBox
Me.pbStatus = New System.Windows.Forms.ProgressBar
Me.OpenFileDialog = New System.Windows.Forms.OpenFileDialog
Me.FolderBrowserDialog = New System.Windows.Forms.FolderBrowserDialog
Me.Button1 = New System.Windows.Forms.Button
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.SuspendLayout()
'
'TabControl1
'
Me.TabControl1.Controls.Add(Me.TabPage1)
Me.TabControl1.Controls.Add(Me.TabPage2)
Me.TabControl1.Location = New System.Drawing.Point(1, 222)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(384, 162)
Me.TabControl1.TabIndex = 0
'
'TabPage1
'
Me.TabPage1.BackColor = System.Drawing.SystemColors.Control
Me.TabPage1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.TabPage1.Controls.Add(Me.btnChangeEncrypt)
Me.TabPage1.Controls.Add(Me.txtDestinationEncrypt)
Me.TabPage1.Controls.Add(Me.Label4)
Me.TabPage1.Controls.Add(Me.btnEncrypt)
Me.TabPage1.Controls.Add(Me.txtConPassEncrypt)
Me.TabPage1.Controls.Add(Me.txtPassEncrypt)
Me.TabPage1.Controls.Add(Me.Label1)
Me.TabPage1.Controls.Add(Me.Label3)
Me.TabPage1.Controls.Add(Me.Label2)
Me.TabPage1.Controls.Add(Me.btnBrowseEncrypt)
Me.TabPage1.Controls.Add(Me.txtFileToEncrypt)
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(376, 136)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "Encrypt File"
'
'btnChangeEncrypt
'
Me.btnChangeEncrypt.BackColor = System.Drawing.Color.DimGray
Me.btnChangeEncrypt.Enabled = False
Me.btnChangeEncrypt.ForeColor = System.Drawing.Color.White
Me.btnChangeEncrypt.Location = New System.Drawing.Point(296, 32)
Me.btnChangeEncrypt.Name = "btnChangeEncrypt"
Me.btnChangeEncrypt.Size = New System.Drawing.Size(72, 21)
Me.btnChangeEncrypt.TabIndex = 11
Me.btnChangeEncrypt.Text = "Change"
Me.btnChangeEncrypt.UseVisualStyleBackColor = False
'
'txtDestinationEncrypt
'
Me.txtDestinationEncrypt.Location = New System.Drawing.Point(104, 32)
Me.txtDestinationEncrypt.Name = "txtDestinationEncrypt"
Me.txtDestinationEncrypt.ReadOnly = True
Me.txtDestinationEncrypt.Size = New System.Drawing.Size(184, 20)
Me.txtDestinationEncrypt.TabIndex = 10
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(8, 32)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(88, 16)
Me.Label4.TabIndex = 9
Me.Label4.Text = "File destination:"
Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'btnEncrypt
'
Me.btnEncrypt.BackColor = System.Drawing.Color.Black
Me.btnEncrypt.Enabled = False
Me.btnEncrypt.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnEncrypt.ForeColor = System.Drawing.Color.Lime
Me.btnEncrypt.Location = New System.Drawing.Point(296, 72)
Me.btnEncrypt.Name = "btnEncrypt"
Me.btnEncrypt.Size = New System.Drawing.Size(72, 32)
Me.btnEncrypt.TabIndex = 8
Me.btnEncrypt.Text = "Encrypt"
Me.btnEncrypt.UseVisualStyleBackColor = False
'
'txtConPassEncrypt
'
Me.txtConPassEncrypt.BackColor = System.Drawing.Color.Black
Me.txtConPassEncrypt.ForeColor = System.Drawing.Color.Red
Me.txtConPassEncrypt.Location = New System.Drawing.Point(104, 88)
Me.txtConPassEncrypt.Name = "txtConPassEncrypt"
Me.txtConPassEncrypt.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.txtConPassEncrypt.Size = New System.Drawing.Size(184, 20)
Me.txtConPassEncrypt.TabIndex = 7
'
'txtPassEncrypt
'
Me.txtPassEncrypt.BackColor = System.Drawing.Color.Black
Me.txtPassEncrypt.ForeColor = System.Drawing.Color.Lime
Me.txtPassEncrypt.Location = New System.Drawing.Point(104, 64)
Me.txtPassEncrypt.Name = "txtPassEncrypt"
Me.txtPassEncrypt.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.txtPassEncrypt.Size = New System.Drawing.Size(184, 20)
Me.txtPassEncrypt.TabIndex = 6
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(18, 10)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(80, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "File to encrypt:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(-8, 88)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(104, 16)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Confirm password:"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 64)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(88, 16)
Me.Label2.TabIndex = 3
Me.Label2.Text = "Type password:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'btnBrowseEncrypt
'
Me.btnBrowseEncrypt.BackColor = System.Drawing.Color.Black
Me.btnBrowseEncrypt.ForeColor = System.Drawing.Color.Red
Me.btnBrowseEncrypt.Location = New System.Drawing.Point(296, 8)
Me.btnBrowseEncrypt.Name = "btnBrowseEncrypt"
Me.btnBrowseEncrypt.Size = New System.Drawing.Size(72, 21)
Me.btnBrowseEncrypt.TabIndex = 2
Me.btnBrowseEncrypt.Text = "Browse"
Me.btnBrowseEncrypt.UseVisualStyleBackColor = False
'
'txtFileToEncrypt
'
Me.txtFileToEncrypt.BackColor = System.Drawing.Color.Black
Me.txtFileToEncrypt.ForeColor = System.Drawing.Color.Lime
Me.txtFileToEncrypt.Location = New System.Drawing.Point(104, 8)
Me.txtFileToEncrypt.Name = "txtFileToEncrypt"
Me.txtFileToEncrypt.ReadOnly = True
Me.txtFileToEncrypt.Size = New System.Drawing.Size(184, 20)
Me.txtFileToEncrypt.TabIndex = 1
Me.txtFileToEncrypt.Text = "Click Browse to load file."
'
'TabPage2
'
Me.TabPage2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.TabPage2.Controls.Add(Me.btnChangeDecrypt)
Me.TabPage2.Controls.Add(Me.txtDestinationDecrypt)
Me.TabPage2.Controls.Add(Me.Label5)
Me.TabPage2.Controls.Add(Me.btnDecrypt)
Me.TabPage2.Controls.Add(Me.txtConPassDecrypt)
Me.TabPage2.Controls.Add(Me.txtPassDecrypt)
Me.TabPage2.Controls.Add(Me.Label6)
Me.TabPage2.Controls.Add(Me.Label7)
Me.TabPage2.Controls.Add(Me.Label8)
Me.TabPage2.Controls.Add(Me.btnBrowseDecrypt)
Me.TabPage2.Controls.Add(Me.txtFileToDecrypt)
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(376, 136)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "Decrypt File"
'
'btnChangeDecrypt
'
Me.btnChangeDecrypt.BackColor = System.Drawing.Color.Gray
Me.btnChangeDecrypt.Enabled = False
Me.btnChangeDecrypt.ForeColor = System.Drawing.Color.White
Me.btnChangeDecrypt.Location = New System.Drawing.Point(296, 32)
Me.btnChangeDecrypt.Name = "btnChangeDecrypt"
Me.btnChangeDecrypt.Size = New System.Drawing.Size(72, 21)
Me.btnChangeDecrypt.TabIndex = 22
Me.btnChangeDecrypt.Text = "Change"
Me.btnChangeDecrypt.UseVisualStyleBackColor = False
'
'txtDestinationDecrypt
'
Me.txtDestinationDecrypt.Location = New System.Drawing.Point(104, 32)
Me.txtDestinationDecrypt.Name = "txtDestinationDecrypt"
Me.txtDestinationDecrypt.ReadOnly = True
Me.txtDestinationDecrypt.Size = New System.Drawing.Size(184, 20)
Me.txtDestinationDecrypt.TabIndex = 21
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(8, 32)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(88, 16)
Me.Label5.TabIndex = 20
Me.Label5.Text = "File destination:"
Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'btnDecrypt
'
Me.btnDecrypt.BackColor = System.Drawing.Color.Black
Me.btnDecrypt.Enabled = False
Me.btnDecrypt.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnDecrypt.ForeColor = System.Drawing.Color.Lime
Me.btnDecrypt.Location = New System.Drawing.Point(296, 72)
Me.btnDecrypt.Name = "btnDecrypt"
Me.btnDecrypt.Size = New System.Drawing.Size(72, 32)
Me.btnDecrypt.TabIndex = 19
Me.btnDecrypt.Text = "Decrypt"
Me.btnDecrypt.UseVisualStyleBackColor = False
'
'txtConPassDecrypt
'
Me.txtConPassDecrypt.BackColor = System.Drawing.Color.Black
Me.txtConPassDecrypt.ForeColor = System.Drawing.Color.Red
Me.txtConPassDecrypt.Location = New System.Drawing.Point(104, 88)
Me.txtConPassDecrypt.Name = "txtConPassDecrypt"
Me.txtConPassDecrypt.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.txtConPassDecrypt.Size = New System.Drawing.Size(184, 20)
Me.txtConPassDecrypt.TabIndex = 18
'
'txtPassDecrypt
'
Me.txtPassDecrypt.BackColor = System.Drawing.Color.Black
Me.txtPassDecrypt.ForeColor = System.Drawing.Color.Lime
Me.txtPassDecrypt.Location = New System.Drawing.Point(104, 64)
Me.txtPassDecrypt.Name = "txtPassDecrypt"
Me.txtPassDecrypt.PasswordChar = Global.Microsoft.VisualBasic.ChrW(42)
Me.txtPassDecrypt.Size = New System.Drawing.Size(184, 20)
Me.txtPassDecrypt.TabIndex = 17
'
'Label6
'
Me.Label6.Location = New System.Drawing.Point(16, 8)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(80, 16)
Me.Label6.TabIndex = 16
Me.Label6.Text = "File to decrypt:"
Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label7
'
Me.Label7.Location = New System.Drawing.Point(-8, 88)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(104, 16)
Me.Label7.TabIndex = 15
Me.Label7.Text = "Confirm password:"
Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'Label8
'
Me.Label8.Location = New System.Drawing.Point(8, 64)
Me.Label8.Name = "Label8"
Me.Label8.Size = New System.Drawing.Size(88, 16)
Me.Label8.TabIndex = 14
Me.Label8.Text = "Type password:"
Me.Label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight
'
'btnBrowseDecrypt
'
Me.btnBrowseDecrypt.BackColor = System.Drawing.Color.Black
Me.btnBrowseDecrypt.ForeColor = System.Drawing.Color.Red
Me.btnBrowseDecrypt.Location = New System.Drawing.Point(296, 8)
Me.btnBrowseDecrypt.Name = "btnBrowseDecrypt"
Me.btnBrowseDecrypt.Size = New System.Drawing.Size(72, 21)
Me.btnBrowseDecrypt.TabIndex = 13
Me.btnBrowseDecrypt.Text = "Browse"
Me.btnBrowseDecrypt.UseVisualStyleBackColor = False
'
'txtFileToDecrypt
'
Me.txtFileToDecrypt.BackColor = System.Drawing.Color.Black
Me.txtFileToDecrypt.ForeColor = System.Drawing.Color.Lime
Me.txtFileToDecrypt.Location = New System.Drawing.Point(104, 8)
Me.txtFileToDecrypt.Name = "txtFileToDecrypt"
Me.txtFileToDecrypt.ReadOnly = True
Me.txtFileToDecrypt.Size = New System.Drawing.Size(184, 20)
Me.txtFileToDecrypt.TabIndex = 12
Me.txtFileToDecrypt.Text = "Click Browse to load file."
'
'pbStatus
'
Me.pbStatus.Location = New System.Drawing.Point(1, 390)
Me.pbStatus.Name = "pbStatus"
Me.pbStatus.Size = New System.Drawing.Size(384, 16)
Me.pbStatus.TabIndex = 1
'
'Button1
'
Me.Button1.BackColor = System.Drawing.Color.Black
Me.Button1.ForeColor = System.Drawing.Color.Red
Me.Button1.Location = New System.Drawing.Point(-8, -3)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(53, 21)
Me.Button1.TabIndex = 2
Me.Button1.Text = "EXIT"
Me.Button1.UseVisualStyleBackColor = False
'
'frmMain
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackgroundImage = Global.EncryptFile.My.Resources.Resources.nx98o1
Me.ClientSize = New System.Drawing.Size(376, 401)
Me.ControlBox = False
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.pbStatus)
Me.Controls.Add(Me.TabControl1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "frmMain"
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Encrypt / Decrypt File by !C3M@N"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.TabPage1.PerformLayout()
Me.TabPage2.ResumeLayout(False)
Me.TabPage2.PerformLayout()
Me.ResumeLayout(False)
End Sub
#End Region
#Region "1. Global Variables "
'*************************
'** Global Variables
'*************************
Dim strFileToEncrypt As String
Dim strFileToDecrypt As String
Dim strOutputEncrypt As String
Dim strOutputDecrypt As String
Dim fsInput As System.IO.FileStream
Dim fsOutput As System.IO.FileStream
#End Region
#Region "2. Create A Key "
'*************************
'** Create A Key
'*************************
Private Function CreateKey(ByVal strPassword As String) As Byte()
'Convert strPassword to an array and store in chrData.
Dim chrData() As Char = strPassword.ToCharArray
'Use intLength to get strPassword size.
Dim intLength As Integer = chrData.GetUpperBound(0)
'Declare bytDataToHash and make it the same size as chrData.
Dim bytDataToHash(intLength) As Byte
'Use For Next to convert and store chrData into bytDataToHash.
For i As Integer = 0 To chrData.GetUpperBound(0)
bytDataToHash(i) = CByte(Asc(chrData(i)))
Next
'Declare what hash to use.
Dim SHA512 As New System.Security.Cryptography.SHA512Managed
'Declare bytResult, Hash bytDataToHash and store it in bytResult.
Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)
'Declare bytKey(31). It will hold 256 bits.
Dim bytKey(31) As Byte
'Use For Next to put a specific size (256 bits) of
'bytResult into bytKey. The 0 To 31 will put the first 256 bits
'of 512 bits into bytKey.
For i As Integer = 0 To 31
bytKey(i) = bytResult(i)
Next
Return bytKey 'Return the key.
End Function
#End Region
#Region "3. Create An IV "
'*************************
'** Create An IV
'*************************
Private Function CreateIV(ByVal strPassword As String) As Byte()
'Convert strPassword to an array and store in chrData.
Dim chrData() As Char = strPassword.ToCharArray
'Use intLength to get strPassword size.
Dim intLength As Integer = chrData.GetUpperBound(0)
'Declare bytDataToHash and make it the same size as chrData.
Dim bytDataToHash(intLength) As Byte
'Use For Next to convert and store chrData into bytDataToHash.
For i As Integer = 0 To chrData.GetUpperBound(0)
bytDataToHash(i) = CByte(Asc(chrData(i)))
Next
'Declare what hash to use.
Dim SHA512 As New System.Security.Cryptography.SHA512Managed
'Declare bytResult, Hash bytDataToHash and store it in bytResult.
Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash)
'Declare bytIV(15). It will hold 128 bits.
Dim bytIV(15) As Byte
'Use For Next to put a specific size (128 bits) of
'bytResult into bytIV. The 0 To 30 for bytKey used the first 256 bits.
'of the hashed password. The 32 To 47 will put the next 128 bits into bytIV.
For i As Integer = 32 To 47
bytIV(i - 32) = bytResult(i)
Next
Return bytIV 'return the IV
End Function
#End Region
#Region "4. Encrypt / Decrypt File "
'****************************
'** Encrypt/Decrypt File
'****************************
Private Enum CryptoAction
'Define the enumeration for CryptoAction.
ActionEncrypt = 1
ActionDecrypt = 2
End Enum
Private Sub EncryptOrDecryptFile(ByVal strInputFile As String, _
ByVal strOutputFile As String, _
ByVal bytKey() As Byte, _
ByVal bytIV() As Byte, _
ByVal Direction As CryptoAction)
Try 'In case of errors.
'Setup file streams to handle input and output.
fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _
FileAccess.Read)
fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, _
FileAccess.Write)
fsOutput.SetLength(0) 'make sure fsOutput is empty
'Declare variables for encrypt/decrypt process.
Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing
Dim lngBytesProcessed As Long = 0 'running count of bytes processed
Dim lngFileLength As Long = fsInput.Length 'the input file's length
Dim intBytesInCurrentBlock As Integer 'current bytes being processed
Dim csCryptoStream As CryptoStream
'Declare your CryptoServiceProvider.
Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged
'Setup Progress Bar
pbStatus.Value = 0
pbStatus.Maximum = 100
'Determine if ecryption or decryption and setup CryptoStream.
Select Case Direction
Case CryptoAction.ActionEncrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateEncryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
Case CryptoAction.ActionDecrypt
csCryptoStream = New CryptoStream(fsOutput, _
cspRijndael.CreateDecryptor(bytKey, bytIV), _
CryptoStreamMode.Write)
End Select
'Use While to loop until all of the file is processed.
While lngBytesProcessed < lngFileLength
'Read file with the input filestream.
intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096)
'Write output file with the cryptostream.
csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock)
'Update lngBytesProcessed
lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock)
'Update Progress Bar
pbStatus.Value = CInt((lngBytesProcessed / lngFileLength) * 100)
End While
'Close FileStreams and CryptoStream.
csCryptoStream.Close()
fsInput.Close()
fsOutput.Close()
'If encrypting then delete the original unencrypted file.
If Direction = CryptoAction.ActionEncrypt Then
Dim fileOriginal As New FileInfo(strFileToEncrypt)
fileOriginal.Delete()
End If
'If decrypting then delete the encrypted file.
If Direction = CryptoAction.ActionDecrypt Then
Dim fileEncrypted As New FileInfo(strFileToDecrypt)
fileEncrypted.Delete()
End If
'Update the user when the file is done.
Dim Wrap As String = Chr(13) + Chr(10)
If Direction = CryptoAction.ActionEncrypt Then
MsgBox("Encryption Complete" + Wrap + Wrap + _
"Total bytes processed = " + _
lngBytesProcessed.ToString, _
MsgBoxStyle.Information, "Done")
'Update the progress bar and textboxes.
pbStatus.Value = 0
txtFileToEncrypt.Text = "Click Browse to load file."
txtPassEncrypt.Text = ""
txtConPassEncrypt.Text = ""
txtDestinationEncrypt.Text = ""
btnChangeEncrypt.Enabled = False
btnEncrypt.Enabled = False
Else
'Update the user when the file is done.
MsgBox("Decryption Complete" + Wrap + Wrap + _
"Total bytes processed = " + _
lngBytesProcessed.ToString, _
MsgBoxStyle.Information, "Done")
'Update the progress bar and textboxes.
pbStatus.Value = 0
txtFileToDecrypt.Text = "Click Browse to load file."
txtPassDecrypt.Text = ""
txtConPassDecrypt.Text = ""
txtDestinationDecrypt.Text = ""
btnChangeDecrypt.Enabled = False
btnDecrypt.Enabled = False
End If
'Catch file not found error.
Catch When Err.Number = 53 'if file not found
MsgBox("Please check to make sure the path and filename" + _
"are correct and if the file exists.", _
MsgBoxStyle.Exclamation, "Invalid Path or Filename")
'Catch all other errors. And delete partial files.
Catch
fsInput.Close()
fsOutput.Close()
If Direction = CryptoAction.ActionDecrypt Then
Dim fileDelete As New FileInfo(txtDestinationDecrypt.Text)
fileDelete.Delete()
pbStatus.Value = 0
txtPassDecrypt.Text = ""
txtConPassDecrypt.Text = ""
MsgBox("Please check to make sure that you entered the correct" + _
"password.", MsgBoxStyle.Exclamation, "Invalid Password")
Else
Dim fileDelete As New FileInfo(txtDestinationEncrypt.Text)
fileDelete.Delete()
pbStatus.Value = 0
txtPassEncrypt.Text = ""
txtConPassEncrypt.Text = ""
MsgBox("This file cannot be encrypted.", _
MsgBoxStyle.Exclamation, "Invalid File")
End If
End Try
End Sub
#End Region
#Region "5. Browse / Change Button "
'******************************
'** Browse/Change Buttons
'******************************
Private Sub btnBrowseEncrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnBrowseEncrypt.Click
'Setup the open dialog.
OpenFileDialog.FileName = ""
OpenFileDialog.Title = "Choose a file to encrypt"
OpenFileDialog.InitialDirectory = "E:"
OpenFileDialog.Filter = "All Files (*.*) | *.*"
'Find out if the user chose a file.
If OpenFileDialog.ShowDialog = DialogResult.OK Then
strFileToEncrypt = OpenFileDialog.FileName
txtFileToEncrypt.Text = strFileToEncrypt
Dim iPosition As Integer = 0
Dim i As Integer = 0
'Get the position of the last "" in the OpenFileDialog.FileName path.
'-1 is when the character your searching for is not there.
'IndexOf searches from left to right.
While strFileToEncrypt.IndexOf(""c, i) <> -1
iPosition = strFileToEncrypt.IndexOf(""c, i)
i = iPosition + 1
End While
'Assign strOutputFile to the position after the last "" in the path.
'This position is the beginning of the file name.
strOutputEncrypt = strFileToEncrypt.Substring(iPosition + 1)
'Assign S the entire path, ending at the last "".
Dim S As String = strFileToEncrypt.Substring(0, iPosition + 1)
'Replace the "." in the file extension with "_".
strOutputEncrypt = strOutputEncrypt.Replace("."c, "_"c)
'The final file name. XXXXX.encrypt
txtDestinationEncrypt.Text = S + strOutputEncrypt + ".encrypt"
'Update buttons.
btnEncrypt.Enabled = True
btnChangeEncrypt.Enabled = True
End If
End Sub
Private Sub btnBrowseDecrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnBrowseDecrypt.Click
'Setup the open dialog.
OpenFileDialog.FileName = ""
OpenFileDialog.Title = "Choose a file to decrypt"
OpenFileDialog.InitialDirectory = "E:"
OpenFileDialog.Filter = "Encrypted Files (*.encrypt) | *.encrypt"
'Find out if the user chose a file.
If OpenFileDialog.ShowDialog = DialogResult.OK Then
strFileToDecrypt = OpenFileDialog.FileName
txtFileToDecrypt.Text = strFileToDecrypt
Dim iPosition As Integer = 0
Dim i As Integer = 0
'Get the position of the last "" in the OpenFileDialog.FileName path.
'-1 is when the character your searching for is not there.
'IndexOf searches from left to right.
While strFileToDecrypt.IndexOf(""c, i) <> -1
iPosition = strFileToDecrypt.IndexOf(""c, i)
i = iPosition + 1
End While
'strOutputFile = the file path minus the last 8 characters (.encrypt)
strOutputDecrypt = strFileToDecrypt.Substring(0, strFileToDecrypt.Length - 8)
'Assign S the entire path, ending at the last "".
Dim S As String = strFileToDecrypt.Substring(0, iPosition + 1)
'Assign strOutputFile to the position after the last "" in the path.
strOutputDecrypt = strOutputDecrypt.Substring((iPosition + 1))
'Replace "_" with "."
txtDestinationDecrypt.Text = S + strOutputDecrypt.Replace("_"c, "."c)
'Update buttons
btnDecrypt.Enabled = True
btnChangeDecrypt.Enabled = True
End If
End Sub
Private Sub btnChangeEncrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnChangeEncrypt.Click
'Setup up folder browser.
FolderBrowserDialog.Description = "Select a folder to place the encrypted file in."
'If the user selected a folder assign the path to txtDestinationEncrypt.
If FolderBrowserDialog.ShowDialog = DialogResult.OK Then
txtDestinationEncrypt.Text = FolderBrowserDialog.SelectedPath + _
"" + strOutputEncrypt + ".encrypt"
End If
End Sub
Private Sub btnChangeDecrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnChangeDecrypt.Click
'Setup up folder browser.
FolderBrowserDialog.Description = "Select a folder for to place the decrypted file in."
'If the user selected a folder assign the path to txtDestinationDecrypt.
If FolderBrowserDialog.ShowDialog = DialogResult.OK Then
txtDestinationDecrypt.Text = FolderBrowserDialog.SelectedPath + _
"" + strOutputDecrypt.Replace("_"c, "."c)
End If
End Sub
#End Region
#Region "6. Encrypt / Decrypt Buttons "
'******************************
'** Encrypt/Decrypt Buttons
'******************************
Private Sub btnEncrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnEncrypt.Click
'Make sure the password is correct.
If txtConPassEncrypt.Text = txtPassEncrypt.Text Then
'Declare variables for the key and iv.
'The key needs to hold 256 bits and the iv 128 bits.
Dim bytKey As Byte()
Dim bytIV As Byte()
'Send the password to the CreateKey function.
bytKey = CreateKey(txtPassEncrypt.Text)
'Send the password to the CreateIV function.
bytIV = CreateIV(txtPassEncrypt.Text)
'Start the encryption.
EncryptOrDecryptFile(strFileToEncrypt, txtDestinationEncrypt.Text, _
bytKey, bytIV, CryptoAction.ActionEncrypt)
Else
MsgBox("Please re-enter your password.", MsgBoxStyle.Exclamation)
txtPassEncrypt.Text = ""
txtConPassEncrypt.Text = ""
End If
End Sub
Private Sub btnDecrypt_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles btnDecrypt.Click
'Make sure the password is correct.
If txtConPassDecrypt.Text = txtPassDecrypt.Text Then
'Declare variables for the key and iv.
'The key needs to hold 256 bits and the iv 128 bits.
Dim bytKey As Byte()
Dim bytIV As Byte()
'Send the password to the CreateKey function.
bytKey = CreateKey(txtPassDecrypt.Text)
'Send the password to the CreateIV function.
bytIV = CreateIV(txtPassDecrypt.Text)
'Start the decryption.
EncryptOrDecryptFile(strFileToDecrypt, txtDestinationDecrypt.Text, _
bytKey, bytIV, CryptoAction.ActionDecrypt)
Else
MsgBox("Please re-enter your password.", MsgBoxStyle.Exclamation)
txtPassDecrypt.Text = ""
txtConPassDecrypt.Text = ""
End If
End Sub
#End Region
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
End Sub
Private Sub txtFileToEncrypt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFileToEncrypt.TextChanged
End Sub
Private Sub pbStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbStatus.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.ShowDialog()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Computer.Audio.Play(My.Resources.eye_of_the_tiger, AudioPlayMode.Background)
End Sub
End Class
Hope It helped