Aide sur le cryptage d'un fichier

sobhi9009 - 22 nov. 2016 à 17:27
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 - 23 nov. 2016 à 08:08
Bonjour tout le monde,
Je suis débutant en VB.net, et je souhaite mettre en place un script qui va décrypter un fichier texte.
Le cryptage de ce fichier a été fait en AES symétrique, et voici le script de décryptage:

Imports System.IO
Imports System.Security.Cryptography

Public Class Form1

Private Shared Function Assign(Of T)(ByRef source As T, ByVal value As T) As T
source = value
Return value
End Function

Private Sub Decrypt(inputFilePath As String, outputfilePath As String)
Dim EncryptionKey As String = "MAKV2SPBNI99212"
Using encryptor As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D,
&H65, &H64, &H76, &H65, &H64, &H65,
&H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
Using fs As New FileStream(inputFilePath, FileMode.Open)
Using cs As New CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
Using fsOutput As New FileStream(outputfilePath, FileMode.Create)
Dim data As Integer
While (Assign(data, cs.ReadByte())) <> -1
fsOutput.WriteByte(CByte(data))
End While
End Using
End Using
End Using
End Using
End Sub
End Class


Ma question semble un peu bête, comment spécifier le fichier d'entrée (document crypté) et le fichier de sortie (document décrypté) ?

PS: J'ai essayé de modifier les valeurs "inputFilePath" et "outputFilePath" par le chemin du fichier, mais cela n'aboutit pas au résultat.

Merci pour votre aide,

2 réponses

vb95 Messages postés 3472 Date d'inscription samedi 11 janvier 2014 Statut Contributeur Dernière intervention 13 avril 2024 169
22 nov. 2016 à 22:24
Bonjour
Ce n'est pas le chemin du fichier qu'il faut mais le nom complet du fichier ( répertoire et fichier )
Le chemin comme tu dis n'est que la succession de répertoire et sous-répertoires le cas écéant
0
cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
23 nov. 2016 à 08:08
Bonjour,

comme ceci:

Imports System.Security.Cryptography
Imports System.IO
'http://www.aspsnippets.com/Articles/Encrypt-and-Decrypt-XML-File-using-C-and-VBNet.aspx
Public Class Form1
    Private Shared Function Assign(Of T)(ByRef source As T, ByVal value As T) As T
        source = value
        Return value
    End Function
    Private Sub Encrypt(inputFilePath As String, outputfilePath As String)
        Dim EncryptionKey As String = "MAKV2SPBNI99212"
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
             &H65, &H64, &H76, &H65, &H64, &H65, _
             &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using fs As New FileStream(outputfilePath, FileMode.Create)
                Using cs As New CryptoStream(fs, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                    Using fsInput As New FileStream(inputFilePath, FileMode.Open)
                        Dim data As Integer
                        While (Assign(data, fsInput.ReadByte())) <> -1
                            cs.WriteByte(CByte(data))
                        End While
                    End Using
                End Using
            End Using
        End Using
    End Sub
    Private Sub Decrypt(inputFilePath As String, outputfilePath As String)
        Dim EncryptionKey As String = "MAKV2SPBNI99212"
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
             &H65, &H64, &H76, &H65, &H64, &H65, _
             &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using fs As New FileStream(inputFilePath, FileMode.Open)
                Using cs As New CryptoStream(fs, encryptor.CreateDecryptor(), CryptoStreamMode.Read)
                    Using fsOutput As New FileStream(outputfilePath, FileMode.Create)
                        Dim data As Integer
                        While (Assign(data, cs.ReadByte())) <> -1
                            fsOutput.WriteByte(CByte(data))
                        End While
                    End Using
                End Using
            End Using
        End Using
    End Sub
    ' Encrypter
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Encrypt("C:\Users\LePivert\Documents\Logiciels\astlog\readme.txt", "C:\Users\LePivert\Documents\monfichiercode.txt")'adapter les chemins
    End Sub
    ' Decrypter
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Decrypt("C:\Users\LePivert\Documents\monfichiercode.txt", "C:\Users\LePivert\Documents\monfichierdecode.txt")'adapter les chemins
    End Sub
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "Crypter"
        Button2.Text = "Decrypter"
    End Sub
End Class

0
Rejoignez-nous