Imports System.IO Imports System.Security.Cryptography Imports System.Text Public Class StringEncryption Public Shared KeyBase64 As String = "JVr/l75Wv29+d643g7+h89Q2Z4m/a1bY09iKz8Wn6as=" Public Shared IVBase64 As String = "n869V+6/7Q11p049/wE99g==" ''' ''' 使用 AES 加密字符串。 ''' ''' 要加密的明文字符串。 ''' 用于加密的密钥(必须是 AES 密钥大小,例如 128, 192 或 256 位)。 ''' 初始化向量(IV),用于增加加密的随机性(必须是 AES 块大小,即 16 字节)。 ''' Base64 编码的密文字符串。 Public Shared Function EncryptStringAES(plainText As String, KeyBase64 As String, IVBase64 As String) As String Dim Key As Byte() Dim iv As Byte() Try Key = System.Convert.FromBase64String(KeyBase64) iv = System.Convert.FromBase64String(IVBase64) Catch ex As Exception Console.WriteLine("密钥和向量失败 err:", ex.Message) End Try ' 检查输入是否有效 If plainText Is Nothing OrElse plainText.Length <= 0 Then Throw New ArgumentNullException("plainText") End If If Key Is Nothing OrElse Key.Length <= 0 Then Throw New ArgumentNullException("key") End If If iv Is Nothing OrElse iv.Length <= 0 Then Throw New ArgumentNullException("iv") End If If Key.Length <> 16 AndAlso Key.Length <> 24 AndAlso Key.Length <> 32 Then Throw New ArgumentException("密钥长度必须为 128, 192 或 256 位 (16, 24 或 32 字节)。") End If If iv.Length <> 16 Then Throw New ArgumentException("IV 长度必须为 128 位 (16 字节)。") End If Dim encrypted As Byte() ' 使用 AES 加密 Using aesAlg As Aes = Aes.Create() aesAlg.Key = Key aesAlg.IV = iv ' 创建加密器 Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV) ' 使用内存流进行加密 Using msEncrypt As New MemoryStream() Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write) Using swEncrypt As New StreamWriter(csEncrypt) ' 将所有数据写入流。 swEncrypt.Write(plainText) End Using encrypted = msEncrypt.ToArray() End Using End Using End Using ' 返回 Base64 编码的加密字节 Return Convert.ToBase64String(encrypted) End Function ''' ''' 使用 AES 解密字符串。 ''' ''' 要解密的 Base64 编码的密文字符串。 ''' 用于解密的密钥(必须与加密时使用的密钥相同)。 ''' 用于解密的初始化向量(必须与加密时使用的 IV 相同)。 ''' 解密后的明文字符串。 Public Shared Function DecryptStringAES(cipherText As String, keyBase As String, ivBase As String) As String Dim key As Byte() Dim iv As Byte() Try key = Convert.FromBase64String(KeyBase64) iv = Convert.FromBase64String(IVBase64) Catch ex As Exception Console.WriteLine("密钥和向量失败 err:", ex.Message) End Try ' 检查输入是否有效 If cipherText Is Nothing OrElse cipherText.Length <= 0 Then Throw New ArgumentNullException("cipherText") End If If key Is Nothing OrElse key.Length <= 0 Then Throw New ArgumentNullException("key") End If If iv Is Nothing OrElse iv.Length <= 0 Then Throw New ArgumentNullException("iv") End If If key.Length <> 16 AndAlso key.Length <> 24 AndAlso key.Length <> 32 Then Throw New ArgumentException("密钥长度必须为 128, 192 或 256 位 (16, 24 或 32 字节)。") End If If iv.Length <> 16 Then Throw New ArgumentException("IV 长度必须为 128 位 (16 字节)。") End If ' 将 Base64 编码的密文转换为字节数组 Dim encryptedBytes As Byte() = Convert.FromBase64String(cipherText) ' 用于存储解密后的明文 Dim plaintext As String = String.Empty ' 使用 AES 解密 Using aesAlg As Aes = Aes.Create() aesAlg.Key = key aesAlg.IV = iv ' 创建解密器 Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV) ' 使用内存流进行解密 Using msDecrypt As New MemoryStream(encryptedBytes) Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read) Using srDecrypt As New StreamReader(csDecrypt) ' 从解密流中读取所有字节并将其解码为字符串。 plaintext = srDecrypt.ReadToEnd() End Using End Using End Using End Using Return plaintext End Function ''' ''' 生成一个随机的 AES 密钥。 ''' ''' 所需的密钥大小(以位为单位,例如 128, 192 或 256)。 ''' 随机生成的密钥字节数组。 Public Shared Function GenerateRandomKey(keySize As Integer) As Byte() Using aesAlg As Aes = Aes.Create() aesAlg.KeySize = keySize aesAlg.GenerateKey() Return aesAlg.Key End Using End Function ''' ''' 生成一个随机的 AES 初始化向量 (IV)。 ''' ''' 随机生成的 IV 字节数组。 Public Shared Function GenerateRandomIV() As Byte() Using aesAlg As Aes = Aes.Create() aesAlg.GenerateIV() Return aesAlg.IV End Using End Function End Class