2016-11-22 2 views
-1

暗号化と復号化を複数のテキストファイルや画像ファイルの暗号化と復号化に:どのように次のコードは動作します使用して、単一の.txtファイル用のフォルダ内

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 

しかし、複数のファイルを暗号化/復号化するために、元の出力をしようとすると、暗号化/復号化ファイル( "Encrypted.txt")は新しい出力に置き換えられます。

ファイル名が異なる複数の暗号化ファイルを作成するにはどうすればループを作成できますか?

答えて

0

使用カウンタ:0、1、2、3、4 ...ファイル名にカウンターを追加し、各ファイルが書き込まれた後にカウンターをステップ:

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

おそらく呼び出すのは間違いです暗号化されたファイル.txtはテキストではないためです。それらはバイナリデータです。それらをテキストとして使用するには、Base64形式に変換し、復号化する前にバイナリデータに変換する必要があります。

また、ディレクトリ内のすべてのファイルを1つのファイルに圧縮し、その1つのファイルを暗号化します。

関連する問題