2020年5月9日 星期六

vb.net AES字串加密 [Script Encrypt with AES]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Imports System.IO

Imports System.Text

Imports System.Security.Cryptography


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        TextBox2.Text = Me.Encrypt(TextBox1.Text.Trim())

        TextBox3.Text = Me.Decrypt(TextBox2.Text.Trim())

        MsgBox(TextBox2.Text.Length)

        TextBox4.Text = AES_Encrypt(TextBox1.Text.Trim(), "23A0026B640858498A2261A9A2146A4A")

        TextBox5.Text = AES_Decrypt(TextBox4.Text.Trim(), "23A0026B640858498A2261A9A2146A4A")

        MsgBox(TextBox4.Text.Length)

    End Sub


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    Private Function Encrypt(ByVal clearText As String) As String

        Dim EncryptionKey As String = "23A0026B640858498A2261A9A2146A4A"

        Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)

        Using encryptor As Aes = Aes.Create()

            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})

            encryptor.Key = pdb.GetBytes(32)

            encryptor.IV = pdb.GetBytes(16)

            Using ms As New MemoryStream()

                Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)

                    cs.Write(clearBytes, 0, clearBytes.Length)

                    cs.Close()

                End Using

                clearText = Convert.ToBase64String(ms.ToArray())

            End Using

        End Using

        Return clearText

    End Function



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    Private Function Decrypt(ByVal cipherText As String) As String

        Dim EncryptionKey As String = "23A0026B640858498A2261A9A2146A4A"

        Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)

        Using encryptor As Aes = Aes.Create()

            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _

             &H65, &H64, &H76, &H65, &H64, &H65, _

             &H76})

            encryptor.Key = pdb.GetBytes(32)

            encryptor.IV = pdb.GetBytes(16)

            Using ms As New MemoryStream()

                Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)

                    cs.Write(cipherBytes, 0, cipherBytes.Length)

                    cs.Close()

                End Using

                cipherText = Encoding.Unicode.GetString(ms.ToArray())

            End Using

        End Using

        Return cipherText

    End Function


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    Public Function AES_Encrypt(ByVal input As String, ByVal pass As String) As String

        Dim AES As New System.Security.Cryptography.RijndaelManaged

        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider

        Dim encrypted As String = ""

        Try

            Dim hash(31) As Byte

            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))

            Array.Copy(temp, 0, hash, 0, 16)

            Array.Copy(temp, 0, hash, 15, 16)

            AES.Key = hash

            AES.Mode = Security.Cryptography.CipherMode.ECB

            Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor

            Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)

            encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

            Return encrypted

        Catch ex As Exception

        End Try

    End Function

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    Public Function AES_Decrypt(ByVal input As String, ByVal pass As String) As String

        Dim AES As New System.Security.Cryptography.RijndaelManaged

        Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider

        Dim decrypted As String = ""

        Try

            Dim hash(31) As Byte

            Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))

            Array.Copy(temp, 0, hash, 0, 16)

            Array.Copy(temp, 0, hash, 15, 16)

            AES.Key = hash

            AES.Mode = Security.Cryptography.CipherMode.ECB

            Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor

            Dim Buffer As Byte() = Convert.FromBase64String(input)

            decrypted = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))

            Return decrypted

        Catch ex As Exception

        End Try

    End Function

沒有留言:

張貼留言