vbc.exe /target:winexe /out:EncryptDecryptApp.exe /r:System.Windows.Forms.dll /r:System.Drawing.dll EncryptDecryptForm.vb


'ファイルの安居化、復号化

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Imports System.Windows.Forms

Public Class EncryptDecryptForm
    Inherits Form

    Private Sub New()
        Me.Text = "ファイル暗号化・復号化"
        Me.Width = 400
        Me.Height = 200
        Me.AllowDrop = True

        ' フォームのドラッグ&ドロップ設定
        AddHandler Me.DragEnter, AddressOf Form_DragEnter
        AddHandler Me.DragDrop, AddressOf Form_DragDrop
    End Sub

    ' ファイルがドラッグされたとき
    Private Sub Form_DragEnter(sender As Object, e As DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub

    ' ファイルがドロップされたとき
    Private Sub Form_DragDrop(sender As Object, e As DragEventArgs)
        Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())

        Dim operation As DialogResult = MessageBox.Show("暗号化しますか?それとも復号化しますか?", "操作を選択", MessageBoxButtons.YesNo)

        If operation = DialogResult.Yes Then
            ' 暗号化処理
            For Each file In files
                EncryptFile(file, file & ".enc", "password123")
            Next
            MessageBox.Show("暗号化が完了しました。")
        Else
            ' 復号化処理
            For Each file In files
                Dim outputFile As String = Path.GetFileNameWithoutExtension(file)
                DecryptFile(file, outputFile, "password123")
            Next
            MessageBox.Show("復号化が完了しました。")
        End If
    End Sub

    ' 暗号化メソッド
    Private Sub EncryptFile(ByVal inputFile As String, ByVal outputFile As String, ByVal password As String)
        Try
            Dim AES As New RijndaelManaged
            Dim hash_AES As New MD5CryptoServiceProvider
            Dim hash As Byte() = hash_AES.ComputeHash(Encoding.UTF8.GetBytes(password))
            AES.Key = hash
            AES.Mode = CipherMode.CFB

            Dim fsInput As New FileStream(inputFile, FileMode.Open)
            Dim fsOutput As New FileStream(outputFile, FileMode.Create)

            Dim cs As New CryptoStream(fsOutput, AES.CreateEncryptor(), CryptoStreamMode.Write)

            Dim buffer(4096) As Byte
            Dim read As Integer

            Do
                read = fsInput.Read(buffer, 0, buffer.Length)
                If read > 0 Then
                    cs.Write(buffer, 0, read)
                End If
            Loop While read > 0

            fsInput.Close()
            cs.Close()
            fsOutput.Close()

        Catch ex As Exception
            MessageBox.Show("エラーが発生しました: " & ex.Message)
        End Try
    End Sub

    ' 復号化メソッド
    Private Sub DecryptFile(ByVal inputFile As String, ByVal outputFile As String, ByVal password As String)
        Try
            Dim AES As New RijndaelManaged
            Dim hash_AES As New MD5CryptoServiceProvider
            Dim hash As Byte() = hash_AES.ComputeHash(Encoding.UTF8.GetBytes(password))
            AES.Key = hash
            AES.Mode = CipherMode.CFB

            Dim fsInput As New FileStream(inputFile, FileMode.Open)
            Dim fsOutput As New FileStream(outputFile, FileMode.Create)

            Dim cs As New CryptoStream(fsInput, AES.CreateDecryptor(), CryptoStreamMode.Read)

            Dim buffer(4096) As Byte
            Dim read As Integer

            Do
                read = cs.Read(buffer, 0, buffer.Length)
                If read > 0 Then
                    fsOutput.Write(buffer, 0, read)
                End If
            Loop While read > 0

            fsInput.Close()
            cs.Close()
            fsOutput.Close()

        Catch ex As Exception
            MessageBox.Show("エラーが発生しました: " & ex.Message)
        End Try
    End Sub

    ' メインメソッド
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New EncryptDecryptForm())
    End Sub
End Class