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

📄 form1.vb

📁 vb.net编写的数据加密程序
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim EncryptOption As String '定义加密方式

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents RB_BySimple As System.Windows.Forms.RadioButton
    Friend WithEvents RB_ByUserName As System.Windows.Forms.RadioButton
    Friend WithEvents RB_ByRandCode As System.Windows.Forms.RadioButton
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label_Password As System.Windows.Forms.Label
    Friend WithEvents TB_RandCode As System.Windows.Forms.TextBox
    Friend WithEvents TB_Encrypt As System.Windows.Forms.TextBox
    Friend WithEvents TB_Decrypt As System.Windows.Forms.TextBox
    Friend WithEvents Button_Encrypt As System.Windows.Forms.Button
    Friend WithEvents Button_Decrypt As System.Windows.Forms.Button
    Friend WithEvents Button_Clear As System.Windows.Forms.Button
    Friend WithEvents Label_RandCode As System.Windows.Forms.Label
    Friend WithEvents CB_Improved As System.Windows.Forms.CheckBox
    Friend WithEvents TB_UserName As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.CB_Improved = New System.Windows.Forms.CheckBox()
        Me.RB_ByRandCode = New System.Windows.Forms.RadioButton()
        Me.RB_ByUserName = New System.Windows.Forms.RadioButton()
        Me.RB_BySimple = New System.Windows.Forms.RadioButton()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label_Password = New System.Windows.Forms.Label()
        Me.Label_RandCode = New System.Windows.Forms.Label()
        Me.TB_RandCode = New System.Windows.Forms.TextBox()
        Me.TB_Encrypt = New System.Windows.Forms.TextBox()
        Me.TB_Decrypt = New System.Windows.Forms.TextBox()
        Me.Button_Encrypt = New System.Windows.Forms.Button()
        Me.Button_Decrypt = New System.Windows.Forms.Button()
        Me.Button_Clear = New System.Windows.Forms.Button()
        Me.TB_UserName = New System.Windows.Forms.TextBox()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.CB_Improved, Me.RB_ByRandCode, Me.RB_ByUserName, Me.RB_BySimple})
        Me.GroupBox1.Font = New System.Drawing.Font("楷体_GB2312", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.GroupBox1.ForeColor = System.Drawing.Color.Black
        Me.GroupBox1.Location = New System.Drawing.Point(32, 14)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(480, 82)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "加密方式"
        '
        'CB_Improved
        '
        Me.CB_Improved.ForeColor = System.Drawing.Color.Red
        Me.CB_Improved.Location = New System.Drawing.Point(169, 53)
        Me.CB_Improved.Name = "CB_Improved"
        Me.CB_Improved.TabIndex = 3
        Me.CB_Improved.Text = "改进模式"
        '
        'RB_ByRandCode
        '
        Me.RB_ByRandCode.Font = New System.Drawing.Font("宋体", 10.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.RB_ByRandCode.ForeColor = System.Drawing.Color.SeaGreen
        Me.RB_ByRandCode.Location = New System.Drawing.Point(331, 26)
        Me.RB_ByRandCode.Name = "RB_ByRandCode"
        Me.RB_ByRandCode.Size = New System.Drawing.Size(136, 24)
        Me.RB_ByRandCode.TabIndex = 2
        Me.RB_ByRandCode.Text = "使用随机码加密"
        '
        'RB_ByUserName
        '
        Me.RB_ByUserName.Font = New System.Drawing.Font("宋体", 10.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.RB_ByUserName.ForeColor = System.Drawing.Color.SeaGreen
        Me.RB_ByUserName.Location = New System.Drawing.Point(169, 26)
        Me.RB_ByUserName.Name = "RB_ByUserName"
        Me.RB_ByUserName.Size = New System.Drawing.Size(136, 24)
        Me.RB_ByUserName.TabIndex = 1
        Me.RB_ByUserName.Text = "使用用户名加密"
        '
        'RB_BySimple
        '
        Me.RB_BySimple.Checked = True
        Me.RB_BySimple.Font = New System.Drawing.Font("宋体", 10.5!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.RB_BySimple.ForeColor = System.Drawing.Color.SeaGreen
        Me.RB_BySimple.Location = New System.Drawing.Point(11, 27)
        Me.RB_BySimple.Name = "RB_BySimple"
        Me.RB_BySimple.Size = New System.Drawing.Size(112, 24)
        Me.RB_BySimple.TabIndex = 0
        Me.RB_BySimple.TabStop = True
        Me.RB_BySimple.Text = "使用简单加密"
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label1.Location = New System.Drawing.Point(32, 119)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(64, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "用户名:"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label2.Location = New System.Drawing.Point(32, 156)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(64, 23)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "密  码:"
        '
        'Label_Password
        '
        Me.Label_Password.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label_Password.ForeColor = System.Drawing.Color.MediumBlue
        Me.Label_Password.Location = New System.Drawing.Point(104, 156)
        Me.Label_Password.Name = "Label_Password"
        Me.Label_Password.Size = New System.Drawing.Size(104, 23)
        Me.Label_Password.TabIndex = 4
        Me.Label_Password.Text = "07552605"
        '
        'Label_RandCode
        '
        Me.Label_RandCode.Font = New System.Drawing.Font("新宋体", 10.5!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
        Me.Label_RandCode.Location = New System.Drawing.Point(281, 157)
        Me.Label_RandCode.Name = "Label_RandCode"
        Me.Label_RandCode.Size = New System.Drawing.Size(64, 23)
        Me.Label_RandCode.TabIndex = 5
        Me.Label_RandCode.Text = "随机码:"
        Me.Label_RandCode.Visible = False
        '
        'TB_RandCode
        '
        Me.TB_RandCode.Location = New System.Drawing.Point(353, 151)
        Me.TB_RandCode.Name = "TB_RandCode"
        Me.TB_RandCode.ReadOnly = True
        Me.TB_RandCode.Size = New System.Drawing.Size(152, 21)
        Me.TB_RandCode.TabIndex = 6
        Me.TB_RandCode.Text = ""
        Me.TB_RandCode.Visible = False
        '
        'TB_Encrypt
        '
        Me.TB_Encrypt.Location = New System.Drawing.Point(86, 240)
        Me.TB_Encrypt.Multiline = True
        Me.TB_Encrypt.Name = "TB_Encrypt"
        Me.TB_Encrypt.Size = New System.Drawing.Size(128, 128)
        Me.TB_Encrypt.TabIndex = 7
        Me.TB_Encrypt.Text = ""
        '
        'TB_Decrypt
        '
        Me.TB_Decrypt.Location = New System.Drawing.Point(350, 240)
        Me.TB_Decrypt.Multiline = True
        Me.TB_Decrypt.Name = "TB_Decrypt"
        Me.TB_Decrypt.Size = New System.Drawing.Size(128, 128)
        Me.TB_Decrypt.TabIndex = 8
        Me.TB_Decrypt.Text = ""
        '
        'Button_Encrypt
        '
        Me.Button_Encrypt.Location = New System.Drawing.Point(86, 208)
        Me.Button_Encrypt.Name = "Button_Encrypt"
        Me.Button_Encrypt.TabIndex = 9
        Me.Button_Encrypt.Text = "加密"
        '
        'Button_Decrypt
        '
        Me.Button_Decrypt.Location = New System.Drawing.Point(350, 208)
        Me.Button_Decrypt.Name = "Button_Decrypt"
        Me.Button_Decrypt.TabIndex = 10
        Me.Button_Decrypt.Text = "解密"
        '
        'Button_Clear
        '
        Me.Button_Clear.Location = New System.Drawing.Point(246, 280)
        Me.Button_Clear.Name = "Button_Clear"
        Me.Button_Clear.TabIndex = 11
        Me.Button_Clear.Text = "<-清空->"
        '
        'TB_UserName
        '
        Me.TB_UserName.Location = New System.Drawing.Point(104, 112)
        Me.TB_UserName.Name = "TB_UserName"
        Me.TB_UserName.Size = New System.Drawing.Size(128, 21)
        Me.TB_UserName.TabIndex = 12
        Me.TB_UserName.Text = "lipeng@cs-air"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(544, 389)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TB_UserName, Me.Button_Clear, Me.Button_Decrypt, Me.Button_Encrypt, Me.TB_Decrypt, Me.TB_Encrypt, Me.TB_RandCode, Me.Label_RandCode, Me.Label_Password, Me.Label2, Me.Label1, Me.GroupBox1})
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "密码加密"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

#Region "Function CreateRandCode —— 生成指定长度的附加码"
    Private Function CreateRandCode(ByVal CodeLen As Integer) As String
        Dim i, j, AscFirst As Integer
        Dim SeedArr(36) As Char  '10个数字加上26个小写英文字母

        AscFirst = Asc("0")     '开始构建种子数组
        For i = 0 To 9
            SeedArr(i) = Chr(i + AscFirst)
        Next
        AscFirst = Asc("a")
        For i = 10 To 35
            SeedArr(i) = Chr(i + AscFirst - 10)
        Next

        Dim str As String
        str = ""

        Randomize()     '初始化随机数生成器
        For i = 1 To CodeLen
            j = Int(Rnd() * 35)
            'str += "(" + j.ToString + ")" + SeedArr(j)
            str += SeedArr(j)
        Next

        Return str

    End Function
#End Region

#Region "选择不同的加密方式"
    Private Sub RB_BySimple_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RB_BySimple.CheckedChanged
        If RB_BySimple.Checked = True Then
            EncryptOption = "1"

            Label_RandCode.Visible = False
            TB_RandCode.Visible = False
        End If
    End Sub


    Private Sub RB_ByUserName_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RB_ByUserName.CheckedChanged
        If RB_ByUserName.Checked = True Then
            EncryptOption = "2"

            Label_RandCode.Visible = False
            TB_RandCode.Visible = False
        End If
    End Sub


    Private Sub RB_ByRandCode_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RB_ByRandCode.CheckedChanged
        If RB_ByRandCode.Checked = True Then
            EncryptOption = "3"

            Label_RandCode.Visible = True
            TB_RandCode.Visible = True

            TB_RandCode.Text = CreateRandCode(10) '生成10位的随机码

        End If
    End Sub
#End Region

#Region "Sub Button_Encrypt_Click —— 加密 "
    Private Sub Button_Encrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Encrypt.Click
        Dim MyEncrypt As New EncryptText()

        Select Case EncryptOption
            Case "1"
                TB_Encrypt.Text = MyEncrypt.EncryptString(Label_Password.Text, "1")

            Case "2"
                If CB_Improved.Checked = False Then
                    TB_Encrypt.Text = MyEncrypt.EncryptString(Label_Password.Text, TB_Username.Text)
                Else
                    '改进的加密方式
                    TB_Encrypt.Text = MyEncrypt.ImprovedEncryptString(Label_Password.Text, TB_UserName.Text)
                End If

            Case "3"
                If CB_Improved.Checked = False Then
                    TB_Encrypt.Text = MyEncrypt.EncryptString(Label_Password.Text, TB_RandCode.Text)
                Else
                    '改进的加密方式
                    TB_Encrypt.Text = MyEncrypt.ImprovedEncryptString(Label_Password.Text, TB_RandCode.Text)
                End If

        End Select

        MyEncrypt = Nothing
    End Sub
#End Region

#Region "Sub Button_Decrypt_Click —— 解密 "
    Private Sub Button_Decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Decrypt.Click
        Dim MyEncrypt As New EncryptText()

        Select Case EncryptOption
            Case "1"
                TB_Decrypt.Text = MyEncrypt.DecryptString(TB_Encrypt.Text, "1")

            Case "2"
                If CB_Improved.Checked = False Then
                    TB_Decrypt.Text = MyEncrypt.DecryptString(TB_Encrypt.Text, TB_UserName.Text)
                Else
                    '改进的解密方式
                    TB_Decrypt.Text = MyEncrypt.ImprovedDecryptString(TB_Encrypt.Text, TB_UserName.Text)
                End If

            Case "3"
                If CB_Improved.Checked = False Then
                    TB_Decrypt.Text = MyEncrypt.DecryptString(TB_Encrypt.Text, TB_RandCode.Text)
                Else
                    '改进的解密方式
                    TB_Decrypt.Text = MyEncrypt.ImprovedDecryptString(TB_Encrypt.Text, TB_RandCode.Text)
                End If

        End Select

        MyEncrypt = Nothing
    End Sub
#End Region

#Region "Sub Button_Clear_Click —— 清空加密和解密文本框"
    Private Sub Button_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Clear.Click
        TB_Decrypt.Text = ""
        TB_Encrypt.Text = ""
    End Sub
#End Region

End Class

⌨️ 快捷键说明

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