2022年3月4日 星期五

[VB.NET] 簡易加密解密工具

 





Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnEnter.Click
        If txtKey.Text.Trim.Length <> 8 Then
            MsgBox("Key請設定為8位數!")
            Exit Sub
        End If
        txtPassword.BackColor = Color.LightGreen
        txtENPassword.BackColor = Color.White
        txtPassword.Text = Decrypt(txtENPassword.Text.Trim, txtKey.Text.Trim)
    End Sub
    Private Sub btnDeEnter_Click(sender As Object, e As EventArgs) Handles btnDeEnter.Click
        If txtKey.Text.Trim.Length <> 8 Then
            MsgBox("Key請設定為8位數!")
            Exit Sub
        End If
        txtENPassword.BackColor = Color.LightGreen
        txtPassword.BackColor = Color.White
        txtENPassword.Text = Encrypt(txtPassword.Text.Trim, txtKey.Text.Trim)
    End Sub

    Private Sub btnClean_Click(sender As Object, e As EventArgs) Handles btnClean.Click
        txtENPassword.Text = ""
        txtPassword.Text = ""
        txtPassword.BackColor = Color.White
        txtENPassword.BackColor = Color.White
    End Sub


#Region "字串解密"
    Public Function Decrypt(ByVal vsToDecrypt As String, ByVal vsKey As String) As String
        Try
            Dim des As New System.Security.Cryptography.DESCryptoServiceProvider()
            ''把字符串放入byte數組
            Dim len As Integer
            len = vsToDecrypt.Length / 2 - 1
            Dim inputByteArray(len) As Byte
            Dim x, i As Integer
            For x = 0 To len
                i = Convert.ToInt32(vsToDecrypt.Substring(x * 2, 2), 16)
                inputByteArray(x) = CType(i, Byte)
            Next

            ''建立加密對象的密鑰和偏移量,此值重要,不能修改
            des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(vsKey)
            des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(vsKey)

            Dim ms As New System.IO.MemoryStream()
            Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor, System.Security.Cryptography.CryptoStreamMode.Write)

            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()

            Return System.Text.Encoding.Default.GetString(ms.ToArray)
        Catch ex As Exception
            Return ""
        End Try
    End Function
#End Region


#Region "字串加密"
    Public Function Encrypt(ByVal vsToEncrypt As String, ByVal vsKey As String) As String
        Dim des As New System.Security.Cryptography.DESCryptoServiceProvider()
        Dim inputByteArray() As Byte
        inputByteArray = System.Text.Encoding.Default.GetBytes(vsToEncrypt)
        ''建立加密對象的密鑰和偏移量
        ''原文使用ASCIIEncoding.ASCII方法的GetBytes方法
        ''使得輸入密碼必須輸入英文文本
        des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(vsKey)
        des.IV = System.Text.ASCIIEncoding.ASCII.GetBytes(vsKey)
        ''寫二進制數組到加密流
        ''(把內存流中的內容全部寫入)
        Dim ms As New System.IO.MemoryStream()
        Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor, System.Security.Cryptography.CryptoStreamMode.Write)
        ''寫二進制數組到加密流
        ''(把內存流中的內容全部寫入)
        cs.Write(inputByteArray, 0, inputByteArray.Length)
        cs.FlushFinalBlock()
        ''建立輸出字符串
        Dim ret As New System.Text.StringBuilder()
        Dim b As Byte
        For Each b In ms.ToArray()
            ret.AppendFormat("{0:X2}", b)
        Next
        Return ret.ToString()
    End Function
#End Region
End Class