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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
'Exemple pour un fichier txt
'à toi de l'adapter à ton application
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
'L'application doit memoriser en dur la "Cle" pour decrypter ce fichier.
'longueur cle limitee à 8 caracteres max dans Algo DES
Shared sSecretKey As String = "opisamus"
'Pour securite additionnelle, pinner la "Cle" en memoire pour le hack.
Dim gch As GCHandle
'Fichier à encrypter et decrypter
Dim strFichierOrigine As String = ""
'Fichier à encrypter et decrypter
Dim strFichierDest As String = ""
'Fichier sauvegarde de "Cle"
Dim strFichierCle As String = ""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
MessageBox.Show(sSecretKey)
End Sub
Private Sub btnEncrypteFichier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypteFichier.Click
'OUVRIR FICHIER ORIGINE (FICHIER À CRYPTER)
Me.OpenFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFichierOrigine = Me.OpenFileDialog1.FileName
If Len(strFichierOrigine) = 0 Then
Exit Sub
End If
End If
'ET FOURNIR LE NOM DU FICHIER CRYPTE
Me.SaveFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFichierDest = Me.SaveFileDialog1.FileName
If Len(strFichierDest) = 0 Then
Exit Sub
End If
End If
'Pour securite additionnelle, pinner la "Cle" en memoire .
gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned)
'PHASE CRYPTAGE
If Len(strFichierOrigine) <> 0 And Len(strFichierDest) <> 0 Then
'ENCRYPTE FICHIER ORIGINE.
Call EncryptFile(strFichierOrigine, _
strFichierDest, _
sSecretKey)
End If
End Sub
Private Sub btnDecrypterFichier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypterFichier.Click
'OUVRIR FICHIER CRYPTE
Me.OpenFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFichierOrigine = Me.OpenFileDialog1.FileName
If Len(strFichierOrigine) = 0 Then
Exit Sub
End If
End If
'ET FOURNIR LE NOM DU FICHIER QUI SERA DECRYPTE
Me.SaveFileDialog1.Filter = "Fichier Texte(*.txt)|*.txt"
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFichierDest = Me.SaveFileDialog1.FileName
If Len(strFichierDest) = 0 Then
Exit Sub
End If
End If
'PHASE DECRYPTAGE
If Len(strFichierOrigine) <> 0 And Len(strFichierDest) <> 0 Then
'DECRYPTE FICHIER.
Call DecryptFile(strFichierOrigine, _
strFichierDest, _
sSecretKey)
End If
'Supprime "Cle" de la memoire.
ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2)
gch.Free()
End Sub
'------------------------ ENCRYPTFILE -------------------
'parametres: nom fichier à crypter,nom fichier apres cryptage,cle (string)
Private Sub EncryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, ByVal sKey As String)
MessageBox.Show(sKey)
'Cree un FileStream pour lire "Fichier à Crypter".
Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
'Cree un FileStream pour ecrire "Fichier Crypte"
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
'Declare instance de DESCryptoServiceProvider class
Dim DES As DESCryptoServiceProvider = New DESCryptoServiceProvider()
'Une cle 64-bit & un IV sont requis.
'Fournir au Provider de cryptographie la "cle secrete"
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
'Init vector IV.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Cree un DES Cryptor à partir de instance DES.
Dim DESEnCrypte As ICryptoTransform = DES.CreateEncryptor()
'Cree une instance de CryptoStream class
Dim CryptoStreamCrypte As New CryptoStream(fsEncrypted, _
DESEnCrypte, _
CryptoStreamMode.Write)
'Lire "Fichier Origine".
Dim byteArrayInput(fsInput.Length - 1) As Byte
fsInput.Read(byteArrayInput, 0, byteArrayInput.Length)
'Ecrit contenu "Fichier apres Cryptage".
CryptoStreamCrypte.Write(byteArrayInput, 0, byteArrayInput.Length)
CryptoStreamCrypte.Close()
End Sub
'------------------------ SUB DECRYPTFILE-------------------
'parametres: nom fichier à decrypter,nom fichier apres decryptage,cle (string)
Private Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, ByVal sKey As String)
MessageBox.Show(sKey)
'Declare instance de DESCryptoServiceProvider class
Dim DES As New DESCryptoServiceProvider()
'Une cle 64-bit & un IV sont requis.
'Entre la cle secrete pour Algorithme DES.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Init vector IV.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'Cree un FileStream pour lire "Fichier Crypte".
Dim fsRead As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'Cree un DES Decryptor à partir de instance DES.
Dim DESDecrypte As ICryptoTransform = DES.CreateDecryptor()
'Cree un CryptoStream pour lire & faire transformation DES inverse .
Dim CryptoStreamDecrypte As New CryptoStream(fsRead, DESDecrypte, CryptoStreamMode.Read)
'Lit contenu "Fichier Crypte".
Dim fsDecrypted As New StreamWriter(sOutputFilename)
'Ecrit contenu "Fichier Original restitue".
fsDecrypted.Write(New StreamReader(CryptoStreamDecrypte).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
'Appel à cette fonction pour supprimer de la memoire la "cle" apres usage
'par Securite.
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" _
(ByVal Destination As String, ByVal Length As Integer)
End Class |
Partager