⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.vb

📁 Tutorial for Vb .Net for Encryption/Decryption of data
💻 VB
📖 第 1 页 / 共 3 页
字号:
        Me.Name = "frmMain"
        Me.RightToLeft = CType(resources.GetObject("$this.RightToLeft"), System.Windows.Forms.RightToLeft)
        Me.StartPosition = CType(resources.GetObject("$this.StartPosition"), System.Windows.Forms.FormStartPosition)
        Me.Text = resources.GetString("$this.Text")
        Me.Visible = CType(resources.GetObject("$this.Visible"), Boolean)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region " Standard Menu Code "
    ' <System.Diagnostics.DebuggerStepThrough()> has been added to some procedures since they are
    ' not the focus of the demo. Remove them if you wish to debug the procedures.
    ' This code simply shows the About form.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click
        ' Open the About form in Dialog Mode
        Dim frm As New frmAbout()
        frm.ShowDialog(Me)
        frm.Dispose()
    End Sub

    ' This code will close the form.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click
        ' Close the current form
        Me.Close()
    End Sub
#End Region

    Private crpSample As SampleCrypto
    Private FormHasLoaded As Boolean = False
    Private strCurrentKeyFile As String
    Private strSourcePath As String
    Private strRijndaelSaltIVFile As String
    Private strTripleDESSaltIVFile As String

    ' This routine handles the "Create Salt / IV Key" button click event.
    Private Sub btnCreateKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateKey.Click
        Try
            If PasswordIsValid() Then
                crpSample.Password = txtPassword.Text
            Else
                Exit Sub
            End If

            If crpSample.CreateSaltIVFile(strCurrentKeyFile) Then
                MsgBox("Salt and IV successfully generated and saved to a .dat " & vbCrLf & _
                    "file in the Visual Studio .NET Solution root folder.", _
                    MsgBoxStyle.Information, Me.Text)
            End If
        Catch exp As Exception
            MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    ' This routine handles the "Encrypt" and "Decrypt" button click events.
    Private Sub EncryptDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click, btnDecrypt.Click
        Dim btn As Button = CType(sender, Button)

        Try
            If chkAdvanced.Checked Then
                If IsValid() Then
                    With crpSample
                        .SaltIVFile = strCurrentKeyFile
                        .Password = txtPassword.Text
                    End With
                Else
                    Exit Sub
                End If
            End If

            crpSample.SourceFileName = strSourcePath

            If btn.Name = "btnEncrypt" Then
                crpSample.EncryptFile()
            Else
                crpSample.DecryptFile()
            End If

            txtCrypto.Text = ReadFileAsString(strSourcePath)

        Catch expCrypto As CryptographicException
            MsgBox("The file could not be decrypted. Make sure you entered " & _
                "the correct password. " & vbCrLf & "This error can also be caused by changing " & _
                "crypto type between encryption and decryption.", _
                MsgBoxStyle.Critical, Me.Text)
        Catch exp As Exception
            MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    ' This routine handles the "Load" button click event.
    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        With odlgSourceFile
            .InitialDirectory = "C:\"
            ' The file could be of any type. The Filter is restricted to Text Format 
            ' files only for demonstration purposes.
            .Filter = "Text Format (*.txt)|*.txt"
            .FilterIndex = 1

            ' The OpenFileDialog control only has an Open button, not an OK button.
            ' However, there is no DialogResult.Open enum so use DialogResult.OK.
            If .ShowDialog() = DialogResult.OK Then
                Try
                    txtCrypto.Text = _
                       ReadFileAsString(.FileName)
                    strSourcePath = .FileName
                Catch exp As ArgumentException
                    MsgBox(exp.Message, MsgBoxStyle.Critical, Me.Text)
                End Try
            End If
        End With
    End Sub

    ' This routine handles the CheckedChanged event for the "Advanced" checkbox.
    Private Sub chkAdvanced_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAdvanced.CheckedChanged
        If chkAdvanced.Checked Then
            lblPassword.Enabled = True
            txtPassword.Enabled = True
            btnCreateKey.Enabled = True
        Else
            lblPassword.Enabled = False
            txtPassword.Enabled = False
            btnCreateKey.Enabled = False
        End If
    End Sub

    ' This routine handles the Form's Load event, setting up the sample.
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set the default crypto type.
        crpSample = New SampleCrypto("Rijndael")

        ' Set the path to save the key file to the Solution root folder by stripping 
        ' "bin" from the current directory.
        Dim strCurrentDirectory As String = _
            Microsoft.VisualBasic.Left(Environment.CurrentDirectory, _
            Len(Environment.CurrentDirectory) - 3)

        ' Initialize paths for both types of key files.
        strRijndaelSaltIVFile = strCurrentDirectory & "RijndaelSaltIV.dat"
        strTripleDESSaltIVFile = strCurrentDirectory & "TripleDESSaltIV.dat"

        ' Set the current key file path to the key for default crypto type.
        strCurrentKeyFile = strRijndaelSaltIVFile

        ' Call Select() to put focus on the "Encrypt" button and prevent the text in 
        ' the TextBox from being automatically highlighted.
        btnEncrypt.Select()

        FormHasLoaded = True
    End Sub

    ' This routine handles the CheckedChanged event for the RadioButton controls.
    Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optRijndaelAdv.CheckedChanged, optTripleDESAdv.CheckedChanged
        If FormHasLoaded Then
            If optRijndaelAdv.Checked Then
                crpSample = New SampleCrypto("Rijndael")
                strCurrentKeyFile = strRijndaelSaltIVFile
            Else
                crpSample = New SampleCrypto("TripleDES")
                strCurrentKeyFile = strTripleDESSaltIVFile
            End If
        End If
    End Sub

    ' This routine validates all data entry.
    Private Function IsValid() As Boolean
        If Not PasswordIsValid() Then
            Return False
        End If

        If strSourcePath = "" Then
            MsgBox("You must first load a source file!", MsgBoxStyle.Exclamation, Me.Text)
            Return False
        End If

        Return True
    End Function

    ' This routine validates the password.
    Private Function PasswordIsValid() As Boolean
        If Not Regex.IsMatch(txtPassword.Text, "^\s*(\w){8}\s*$") Then
            MsgBox("You must enter an 8-digit password consisting of numbers " & _
                "and/or letters.", MsgBoxStyle.Exclamation, Me.Text)
            Return False
        End If

        Return True
    End Function

    ' This routine reads in the contents of a file and converts it to a string.
    Shared Function ReadFileAsString(ByVal path As String) As String
        Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim abyt(CInt(fs.Length - 1)) As Byte

        fs.Read(abyt, 0, abyt.Length)
        fs.Close()

        Return UTF8.GetString(abyt)
    End Function

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -