Imports System.Security.Cryptography
Imports System.Text
//加密
Public
Class
AESHelper
Public Shared Function EncryptAES128(ByVal plainText As String, ByVal key As String) As String
Dim result As String = ""
Using aes As New AesManaged()
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.PKCS7
aes.Key = Encoding.UTF8.GetBytes(key)
Using encryptor As ICryptoTransform = aes.CreateEncryptor(aes.Key, aes.IV)
Using ms As New System.IO.MemoryStream()
Using cs As New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
Dim plainBytes As Byte() = Encoding.UTF8.GetBytes(plainText)
cs.Write(plainBytes, 0, plainBytes.Length)
cs.FlushFinalBlock()
Dim encryptedBytes As Byte() = ms.ToArray()
result = Convert.ToBase64String(encryptedBytes)
End Using
End Using
End Using
End Using
Return result
End Function
End Class
Imports System.Security.Cryptography
Imports System.Text
//解密
Public Class AESHelper
Public Shared Function DecryptAES128(ByVal encryptedText As String, ByVal key As String) As String
Dim result As String = ""
Using aes As New AesManaged()
aes.Mode = CipherMode.ECB
aes.Padding = PaddingMode.PKCS7
aes.Key = Encoding.UTF8.GetBytes(key)
Using decryptor As ICryptoTransform = aes.CreateDecryptor(aes.Key, aes.IV)
Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedText)
Using ms As New System.IO.MemoryStream(encryptedBytes)
Using cs As New CryptoStream(ms, decryptor, CryptoStreamMode.Read)
Using reader As New System.IO.StreamReader(cs)
result = reader.ReadToEnd()
End Using
End Using
End Using
End Using
End Using
Return result
End Function
End Class