1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Private SecretKey(15) As Byte
Private SecretIV(15) As Byte
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim open As New OpenFileDialog
open.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
open.Filter = "Text Files (.txt)|*.txt"
If open.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = open.FileName
Else
Exit Sub
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim save As New SaveFileDialog
save.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
save.Filter = "Text Files (.txt)|*.txt"
If save.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox2.Text = save.FileName
Else
Exit Sub
End If
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
If TextBox1.Text.Length > 0 OrElse TextBox2.Text.Length > 0 Then
EncryptFile(TextBox1.Text, TextBox2.Text)
MessageBox.Show("fichier crypter!")
Else
MessageBox.Show("Veuillez séléctionner un fichier entrer et sortie !", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub EncryptFile(ByVal InputFilename As String, ByVal OutputFilename As String)
Dim AES As New AesCryptoServiceProvider()
Dim AesDecrypt As ICryptoTransform = AES.CreateEncryptor(SecretKey, SecretIV)
Using fsInput As New FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
Using fsOutput As New FileStream(OutputFilename, FileMode.Create, FileAccess.Write, FileShare.None)
Using cryptostream As New CryptoStream(fsOutput, AesDecrypt, CryptoStreamMode.Write)
Dim bytearrayinput(fsInput.Length - 1) As Byte
Dim bytesRead As Long = 0
bytesRead = fsInput.Read(bytearrayinput, 0, fsInput.Length)
If Not (bytesRead = 0) Then
cryptostream.Write(bytearrayinput, 0, bytesRead)
cryptostream.Flush()
End If
End Using
End Using
End Using
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
If TextBox1.Text.Length > 0 OrElse TextBox2.Text.Length > 0 Then
DécryptFile(TextBox1.Text, TextBox2.Text)
MessageBox.Show("Fichier Décrypter!")
Else
MessageBox.Show("Veuillez séléctionner un fichier entrer et sortie !", "erreur", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub DécryptFile(ByVal InputFilename As String, ByVal OutputFilename As String)
Dim AES As New AesCryptoServiceProvider()
Dim AesDecrypt As ICryptoTransform = AES.CreateDecryptor(SecretKey, SecretIV)
Using fsread As New FileStream(InputFilename, FileMode.Open, FileAccess.Read, FileShare.Read)
Using cryptostreamDecr As New CryptoStream(fsread, AesDecrypt, CryptoStreamMode.Read)
Using fsDecrypted As New FileStream(OutputFilename, FileMode.Create, FileAccess.Write, FileShare.None)
Dim bytearrayinput(fsread.Length) As Byte
Dim bytesRead As Long = 0
bytesRead = cryptostreamDecr.Read(bytearrayinput, 0, fsread.Length)
If (bytesRead > 0) Then
fsDecrypted.Write(bytearrayinput, 0, bytesRead)
fsDecrypted.Flush()
End If
End Using
End Using
End Using
End Sub
End Class |
Partager