164 lines
7.1 KiB
VB.net
164 lines
7.1 KiB
VB.net
|
|
Imports System.Security.Cryptography
|
|||
|
|
Imports System.Text
|
|||
|
|
|
|||
|
|
Namespace Security
|
|||
|
|
|
|||
|
|
Public Class Aes128
|
|||
|
|
'todo:解密秘钥不应出现明文,考虑修改为十六进制数组
|
|||
|
|
''' <summary>服务器密钥</summary>
|
|||
|
|
Public Shared ReadOnly ServerAesKey As String = "^5GHo96tr5ff&*Hphg7665^fyu&uOhj0j[0[(jOIhI&g77FgghO*hhHY8h(*H&-987OygO8yg*yS$&G&aG9*&6g96*&7^RA65s76r*&&G(*(7(Gyg7#7Ff7F*&(*&&^"
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 加密文本为Base64编码
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="source">需要加密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
''' <remarks></remarks>
|
|||
|
|
Public Shared Function EncryptStr(source As String, key As String) As String
|
|||
|
|
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using encrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(Encoding.UTF8.GetBytes(key))
|
|||
|
|
encrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
encrypt.Key = keyBytes
|
|||
|
|
encrypt.IV = keyBytes
|
|||
|
|
encrypt.Mode = CipherMode.CBC
|
|||
|
|
encrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = encrypt.CreateEncryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = Encoding.UTF8.GetBytes(source)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return Convert.ToBase64String(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 加密文本为Base64编码
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="source">需要加密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
Public Shared Function EncryptStr(ByVal source As String, key() As Byte) As String
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using encrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(key)
|
|||
|
|
encrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
encrypt.Key = keyBytes
|
|||
|
|
encrypt.IV = keyBytes
|
|||
|
|
encrypt.Mode = CipherMode.CBC
|
|||
|
|
encrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = encrypt.CreateEncryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = Encoding.UTF8.GetBytes(source)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return Convert.ToBase64String(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 加密文本为Base64编码
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="source">需要加密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <param name="encode">密钥转换字符编码</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
''' <remarks></remarks>
|
|||
|
|
Public Shared Function EncryptStr(ByVal source As String, ByVal key As String, ByVal encode As Encoding) As String
|
|||
|
|
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using encrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(encode.GetBytes(key))
|
|||
|
|
encrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
encrypt.Key = keyBytes
|
|||
|
|
encrypt.IV = keyBytes
|
|||
|
|
encrypt.Mode = CipherMode.CBC
|
|||
|
|
encrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = encrypt.CreateEncryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = encode.GetBytes(source)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return Convert.ToBase64String(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 解密Base64编码的字符串
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="encodedStr">需要解密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
''' <remarks></remarks>
|
|||
|
|
Public Shared Function DecryptStr(ByVal encodedStr As String, ByVal key As String) As String
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using decrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(Encoding.UTF8.GetBytes(key)) '设置密钥
|
|||
|
|
decrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
decrypt.Key = keyBytes
|
|||
|
|
decrypt.IV = keyBytes
|
|||
|
|
decrypt.Mode = CipherMode.CBC
|
|||
|
|
decrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = decrypt.CreateDecryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = Convert.FromBase64String(encodedStr)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return Encoding.UTF8.GetString(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 解密Base64编码的字符串
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="encodedStr">需要解密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
Public Shared Function DecryptStr(encodedStr As String, key() As Byte) As String
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using decrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(key)
|
|||
|
|
decrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
decrypt.Key = keyBytes
|
|||
|
|
decrypt.IV = keyBytes
|
|||
|
|
decrypt.Mode = CipherMode.CBC
|
|||
|
|
decrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = decrypt.CreateDecryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = Convert.FromBase64String(encodedStr)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return Encoding.UTF8.GetString(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
|
|||
|
|
''' <summary>
|
|||
|
|
''' 解密Base64编码的字符串
|
|||
|
|
''' </summary>
|
|||
|
|
''' <param name="encodedStr">需要解密字符串</param>
|
|||
|
|
''' <param name="key">密钥</param>
|
|||
|
|
''' <param name="encode">密钥转换字符编码</param>
|
|||
|
|
''' <returns></returns>
|
|||
|
|
''' <remarks></remarks>
|
|||
|
|
Public Shared Function DecryptStr(ByVal encodedStr As String, ByVal key As String, ByVal encode As Encoding) As String
|
|||
|
|
Dim inBuff As Byte()
|
|||
|
|
Dim crypt As ICryptoTransform
|
|||
|
|
Dim msp As New MD5CryptoServiceProvider
|
|||
|
|
Using decrypt As Aes = Aes.Create("AES")
|
|||
|
|
Dim keyBytes() As Byte = msp.ComputeHash(encode.GetBytes(key)) '设置密钥
|
|||
|
|
decrypt.BlockSize = keyBytes.Length * 8
|
|||
|
|
decrypt.Key = keyBytes
|
|||
|
|
decrypt.IV = keyBytes
|
|||
|
|
decrypt.Mode = CipherMode.CBC
|
|||
|
|
decrypt.Padding = PaddingMode.PKCS7
|
|||
|
|
crypt = decrypt.CreateDecryptor()
|
|||
|
|
End Using
|
|||
|
|
inBuff = Convert.FromBase64String(encodedStr)
|
|||
|
|
msp.Dispose()
|
|||
|
|
Return encode.GetString(crypt.TransformFinalBlock(inBuff, 0, inBuff.Length))
|
|||
|
|
End Function
|
|||
|
|
End Class
|
|||
|
|
End Namespace
|