2016-11-22 2 views
-1

chiffrement et le déchiffrement pour un seul fichier txt en utilisant le code suivant fonctionne:Comment crypter et décrypter plusieurs fichiers texte et image dans les dossiers

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles SelectFile.Click 

    'Create a File Dialog Box to select the source file 
    Dim dlg As New OpenFileDialog 
    'If OK Button is Click then add file name with path to textBox1 
    If dlg.ShowDialog() = DialogResult.OK Then 
     TextBox1.Text = dlg.FileName 
    End If 
End Sub 
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Encrypt.Click 
    Dim outputFile As String 
    outputFile = "M:\Encryptions\Encrypted.txt" 
    Dim fsInput As New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read) 
    Dim fsEncrypted As New FileStream(outputFile, FileMode.Create, FileAccess.Write) 
    Dim sKey As String 
    sKey = "Helloabc" 

    Dim DES As New DESCryptoServiceProvider 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 
    Dim desencrypt As ICryptoTransform 
    desencrypt = DES.CreateEncryptor() 

    Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write) 
    Dim bytearrayinput(fsInput.Length) As Byte 
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) 
    cryptostream.Close() 
    fsInput.Close() 
    fsEncrypted.Close() 
    TextBox2.Text = "M:\Encryptions\Encrypted.txt" 
End Sub 
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Decrypt.Click 
    Dim DES As New DESCryptoServiceProvider 
    Dim sKey As String 
    sKey = "Helloabc" 
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 
    Dim fsread As New FileStream(TextBox2.Text, FileMode.Open, FileAccess.Read) 
    Dim desdecrypt As ICryptoTransform 
    desdecrypt = DES.CreateDecryptor() 

    Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read) 
    Dim fsDecrypted As New StreamWriter("M:\Decryptions\Decrypted.txt") 
    fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd()) 
    fsDecrypted.Flush() 
    fsDecrypted.Close() 
    TextBox3.Text = "M:\Decryptions\Decrypted.txt" 
End Sub 

Cependant lorsque vous essayez de crypter/décrypter des fichiers multiples, la sortie d'origine Le fichier de cryptage/décryptage ("Encrypted.txt") sera remplacé par la nouvelle sortie.

Comment créer une boucle pour créer plusieurs fichiers cryptés avec différents noms de fichiers?

Répondre

0

Utilisez un compteur: 0, 1, 2, 3, 4 ... le compteur Append le nom et l'étape du compteur après chaque fichier est écrit:

Encrypted00.dat 
Encrypted01.dat 
Encrypted02.dat 
Encrypted03.dat 
... 

Il est probablement une erreur d'appeler les fichiers cryptés .txt car ils ne seront pas texte. Ce sont des données binaires. Si vous les voulez en tant que texte, vous devrez convertir au format Base64 et les convertir en données binaires avant de les décrypter.

Vous pouvez également compresser tous les fichiers du répertoire dans un seul fichier et chiffrer ce fichier unique.