Public Shared Function EncryptString(ByVal sInputString As String, ByVal sKey As String, ByVal sIV As String) As String
Try
Dim data As Object = Encoding.UTF8.GetBytes(sInputString)
Dim DES As Object = New DESCryptoServiceProvider()
DES.Key = Encoding.ASCII.GetBytes(sKey)
DES.IV = Encoding.ASCII.GetBytes(sIV)
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
Dim result As Byte() = desencrypt.TransformFinalBlock(data, 0, data.Length)
Return Convert.ToBase64String(result.ToArray())
Catch
Return sInputString
End Try
End Function
Public Shared Function DecryptString(ByVal sInputString As String, ByVal sKey As String, ByVal sIV As String) As String
Try
Dim data As Object = Convert.FromBase64String(sInputString)
Dim DES As Object = New DESCryptoServiceProvider()
DES.Key = Encoding.ASCII.GetBytes(sKey)
DES.IV = Encoding.ASCII.GetBytes(sIV)
Dim desencrypt As ICryptoTransform = DES.CreateDecryptor()
Dim result As Byte() = desencrypt.TransformFinalBlock(data, 0, data.Length)
Return Encoding.[Default].GetString(result)
Catch
Return sInputString
End Try
End Function
Public Function DecryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用标准DES对称解密
Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法
'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法
des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符
des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符
Dim buffer As Byte() = Convert.FromBase64String(SourceStr)
Dim ms As New System.IO.MemoryStream(buffer)
Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read)
Dim sr As New System.IO.StreamReader(cs)
DecryptDes = sr.ReadToEnd()
End Function
Public Function EncryptDes(ByVal SourceStr As String, ByVal myKey As String, ByVal myIV As String) As String '使用的DES对称加密
Dim des As New System.Security.Cryptography.DESCryptoServiceProvider 'DES算法
'Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider'TripleDES算法
Dim inputByteArray As Byte()
inputByteArray = System.Text.Encoding.Default.GetBytes(SourceStr)
des.Key = System.Text.Encoding.UTF8.GetBytes(myKey) 'myKey DES用8个字符,TripleDES要24个字符
des.IV = System.Text.Encoding.UTF8.GetBytes(myIV) 'myIV DES用8个字符,TripleDES要24个字符
Dim ms As New System.IO.MemoryStream
Dim cs As New System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write)
Dim sw As New System.IO.StreamWriter(cs)
sw.Write(SourceStr)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
EncryptDes = Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End Function
[本日志由 tiancao1001 于 2018-12-06 05:23 PM 编辑]
暂时没有评论