2020年5月21日 星期四

vb.net 文字加密 [Encrypt String]


 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Imports System.Data.SqlClient

Imports System.IO

Imports System.Security.Cryptography


Public Class Form1


  Private enc As System.Text.UTF8Encoding

  Private encryptor As ICryptoTransform

  Private decryptor As ICryptoTransform


  Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click

    Dim sPlainText As String = Me.TextBox1.Text

    If Not String.IsNullOrEmpty(sPlainText) Then

      Dim memoryStream As MemoryStream = New MemoryStream()

      Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, Me.encryptor, CryptoStreamMode.Write)

      cryptoStream.Write(Me.enc.GetBytes(sPlainText), 0, sPlainText.Length)

      cryptoStream.FlushFinalBlock()

      Me.TextBox1.Text = Convert.ToBase64String(memoryStream.ToArray())

      memoryStream.Close()

      cryptoStream.Close()

    End If


  End Sub


  Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click

    Dim cypherTextBytes As Byte() = Convert.FromBase64String(Me.TextBox1.Text)

    Dim memoryStream As MemoryStream = New MemoryStream(cypherTextBytes)

    Dim cryptoStream As CryptoStream = New CryptoStream(memoryStream, Me.decryptor, CryptoStreamMode.Read)

    Dim plainTextBytes(cypherTextBytes.Length) As Byte

    Dim decryptedByteCount As Integer = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length)

    memoryStream.Close()

    cryptoStream.Close()

    Me.TextBox1.Text = Me.enc.GetString(plainTextBytes, 0, decryptedByteCount)

  End Sub


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

    Dim KEY_128 As Byte() = {42, 1, 52, 67, 231, 13, 94, 101, 123, 6, 0, 12, 32, 91, 4, 111, 31, 70, 21, 141, 123, 142, 234, 82, 95, 129, 187, 162, 12, 55, 98, 23}

    Dim IV_128 As Byte() = {234, 12, 52, 44, 214, 222, 200, 109, 2, 98, 45, 76, 88, 53, 23, 78}

    Dim symmetricKey As RijndaelManaged = New RijndaelManaged()

    symmetricKey.Mode = CipherMode.CBC


    Me.enc = New System.Text.UTF8Encoding

    Me.encryptor = symmetricKey.CreateEncryptor(KEY_128, IV_128)

    Me.decryptor = symmetricKey.CreateDecryptor(KEY_128, IV_128)

  End Sub


End Class

沒有留言:

張貼留言