使用 Visual Studio 2008 (VB.NET) 與 System.Security.Cryptography 命名空間,實作一個完整的文字加密/解密 WinForms 程式。採用 DES 對稱加密算法,涵蓋介面設計、類別封裝、事件處理、錯誤處理全流程,適合 .NET 入門開發者理解加密基礎與 WinForms 開發模式。
| 控件 | 名稱 | 關鍵屬性 | 用途 |
|---|---|---|---|
| Label | Label1 | Text="原始文字" | 提示標籤 |
| TextBox | TextBox1 | Multiline=True, ScrollBars=Vertical | 輸入待加密文字 |
| Label | Label2 | Text="加密結果" | 提示標籤 |
| TextBox | TextBox2 | Multiline=True, ScrollBars=Vertical, ReadOnly=True | 顯示 Base64 加密串 |
| Label | Label3 | Text="解密結果" | 提示標籤 |
| TextBox | TextBox3 | Multiline=True, ScrollBars=Vertical, ReadOnly=True | 顯示解密後明文 |
| Button | Button1 | Text="加密 (&E)" | 觸發加密 |
| Button | Button2 | Text="解密 (&D)" | 觸發解密 |
CryptoHelperImports System
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class CryptoHelper
' DES 需要 8 字節 Key 和 8 字節 IV
Private Shared ReadOnly KeyBytes As Byte() = Encoding.Default.GetBytes("20000000") ' 8 字節
Private Shared ReadOnly IVBytes As Byte() = Encoding.Default.GetBytes("12345678") ' 8 字節
Public Shared Function Encrypt(plainText As String) As String
If String.IsNullOrEmpty(plainText) Then Return String.Empty
Try
Dim inputBytes = Encoding.Default.GetBytes(plainText)
Using ms As New MemoryStream()
Using des As New DESCryptoServiceProvider()
Using cs As New CryptoStream(ms, des.CreateEncryptor(KeyBytes, IVBytes), CryptoStreamMode.Write)
cs.Write(inputBytes, 0, inputBytes.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
End Using
End Using
End Using
Catch ex As CryptographicException
Throw New ApplicationException("加密失敗: " & ex.Message, ex)
End Try
End Function
Public Shared Function Decrypt(cipherBase64 As String) As String
If String.IsNullOrEmpty(cipherBase64) Then Return String.Empty
Try
Dim inputBytes = Convert.FromBase64String(cipherBase64)
Using ms As New MemoryStream()
Using des As New DESCryptoServiceProvider()
Using cs As New CryptoStream(ms, des.CreateDecryptor(KeyBytes, IVBytes), CryptoStreamMode.Write)
cs.Write(inputBytes, 0, inputBytes.Length)
cs.FlushFinalBlock()
Return Encoding.Default.GetString(ms.ToArray())
End Using
End Using
End Using
Catch ex As CryptographicException
Throw New ApplicationException("解密失敗: " & ex.Message, ex)
End Try
End Function
End Class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
TextBox2.Text = CryptoHelper.Encrypt(TextBox1.Text)
Catch ex As Exception
MessageBox.Show("加密錯誤: " & ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Try
TextBox3.Text = CryptoHelper.Decrypt(TextBox2.Text)
Catch ex As Exception
MessageBox.Show("解密錯誤: " & ex.Message, "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
"20000000"、"12345678",實際專案必須從安全配置/金鑰管理系統讀取。Encoding.UTF8。CryptoStreamMode.Write 表示寫入明文/密文流,FlushFinalBlock() 必須調用以完成填充/終止區塊。IDisposable 資源 (Stream、CryptoServiceProvider、CryptoStream) 及時釋放。Hello, VB.NET 加密測試!xYzAbC123==)
| 層級 | 算法 | Key Size | 備註 |
|---|---|---|---|
| 教學/非敏感 | DES | 56-bit | 本例使用,僅供學習 |
| 生產標準 | AES (Rijndael) | 128/192/256-bit | Aes.Create()、GCM 模式 (需 .NET Core 3.1+) |
| 高安全 | AES-GCM / ChaCha20-Poly1305 | 256-bit | 認證加密 (AEAD),防篡改 |
| 密碼派生 | PBKDF2 / Argon2 | - | 從密碼生成 Key/IV,Rfc2898DeriveBytes |
' 現代寫法更簡潔、安全、跨平台
Using aes = Aes.Create()
aes.Key = keyBytes
aes.IV = ivBytes
Using encryptor = aes.CreateEncryptor()
' ... 同 CryptoStream 模式
End Using
End Using
' 或 .NET 7+ 暫不支援內建 AEAD,需引用 System.Security.Cryptography.Aead (實驗) 或第三方庫
這個範例展示了「加密服務封裝 + WinForms 事件驅動」的經典架構。理解 CryptoStream 流式處理、Base64 編碼、Key/IV 管理,是進階學習 TLS、JWT、數據保護 API 的基石。