AES128加密方法
Dim key As String = "12323123"
Dim data As String = "31231231214124123"
Dim encryptedBytes As Byte()
Dim aesAlg As New System.Security.Cryptography.AesCryptoServiceProvider()
aesAlg.Key = Encoding.ASCII.GetBytes(key)
aesAlg.Mode = System.Security.Cryptography.CipherMode.ECB
aesAlg.Padding = System.Security.Cryptography.PaddingMode.PKCS7 '修改类型
Dim encryptor As System.Security.Cryptography.ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
Dim memoryStream As New System.IO.MemoryStream()
Dim cryptoStream As New System.Security.Cryptography.CryptoStream(memoryStream, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
Dim dataBytes As Byte() = Encoding.ASCII.GetBytes(data)
cryptoStream.Write(dataBytes, 0, dataBytes.Length)
cryptoStream.FlushFinalBlock()
encryptedBytes = memoryStream.ToArray()
Output.Show(Convert.ToBase64String(encryptedBytes))
AES128解密方法
Dim key As String = "3123123"
Dim encryptedData As String = "PNOK3123AJs/9W3fVko19o="
Dim decryptedBytes As Byte()
Dim aesAlg As New System.Security.Cryptography.AesCryptoServiceProvider()
aesAlg.KeySize = 128
aesAlg.Key = Encoding.ASCII.GetBytes(key)
aesAlg.Mode = System.Security.Cryptography.CipherMode.ECB
aesAlg.Padding = System.Security.Cryptography.PaddingMode.PKCS7 '修改类型
Dim decryptor As System.Security.Cryptography.ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)
Dim encryptedBytes As Byte() = Convert.FromBase64String(encryptedData)
Dim memoryStream As New System.IO.MemoryStream(encryptedBytes)
Dim cryptoStream As New System.Security.Cryptography.CryptoStream(memoryStream, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)
Dim decryptedDataBytes As Byte() = New Byte(encryptedBytes.Length - 1) {}
Dim decryptedLength As Integer = cryptoStream.Read(decryptedDataBytes, 0, decryptedDataBytes.Length)
Dim unpaddedDataBytes As Byte() = New Byte(decryptedLength - 1) {}
Array.Copy(decryptedDataBytes, unpaddedDataBytes, decryptedLength)
decryptedBytes = unpaddedDataBytes
Output.Show(System.Text.ASCIIEncoding.ASCII.GetString(decryptedBytes))
[此贴子已经被作者于2023/12/11 8:18:51编辑过]