Imports System.Security Imports System.Security.Cryptography Imports System.Text Imports System.IO Imports System.Object Imports System.Security.Cryptography.SymmetricAlgorithm Imports System.Security.Cryptography.DES Imports System.Security.Cryptography.DESCryptoServiceProvider Public Class Form1 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 String Dim DES1 As DESCryptoServiceProvider = New DESCryptoServiceProvider() Dim DES1Key = ASCIIEncoding.ASCII.GetBytes("12345678") Dim DES1IV = ASCIIEncoding.ASCII.GetBytes("12345678") Dim encryptation1 As ICryptoTransform = DES1.CreateDecryptor(DES1Key, DES1IV) Private Sub btnBrowseEncrypt_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnBrowseEncrypt.Click 'Setup the open dialog. OpenFileDialog1.FileName = "" OpenFileDialog1.Title = "Choose a file to encrypt" OpenFileDialog1.InitialDirectory = "C:\" OpenFileDialog1.Filter = "All Files (*.*) | *.*" 'Find out if the user chose a file. If OpenFileDialog1.ShowDialog = DialogResult.OK Then strFileToEncrypt = OpenFileDialog1.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 "\". 'Update buttons. btnEncrypt.Enabled = True btnChangeEncrypt.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. FolderBrowserDialog1.Description = "Select a folder to place the encrypted file in." 'If the user selected a folder assign the path to txtDestinationEncrypt. If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then txtDestinationEncrypt.Text = FolderBrowserDialog1.SelectedPath End If End Sub Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click Try Dim ProcessFileStream As FileStream = New FileStream(strFileToEncrypt, FileMode.Open, FileAccess.Read) Dim ResultFileStream As FileStream = New FileStream(strOutputEncrypt, FileMode.Create, FileAccess.Write) Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, encryptation1, CryptoStreamMode.Write) Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length) myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length) myCryptoStream.Close() ProcessFileStream.Close() ResultFileStream.Close() Catch ex As Exception Console.WriteLine(ex.Message) End Try Console.ReadLine() End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)