1、DES加密
Public Function EncryptDes(ByVal SourceStr As String, Optional ByVal myKey As String = "", Optional ByVal myIV As String = "") As String '使用的DES對稱加密 If String.IsNullOrEmpty(myKey) Then myKey = Me.JMKey End If If String.IsNullOrEmpty(myIV) Then myIV = Me.JMIv End If Dim des As New System.Security.Cryptography.DESCryptoServicePublic Function DecryptDes(ByVal SourceStr As String, Optional ByVal myKey As String = "", Optional ByVal myIV As String = "") As String '使用標準DES對稱解密 If String.IsNullOrEmpty(SourceStr) Then Return SourceStr End If If SourceStr = "" Then Return SourceStr End If If String.IsNullOrEmpty(myKey) Then myKey = Me.JMKey End If If String.IsNullOrEmpty(myIV) Then myIV = Me.JMIv End If 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() Return DecryptDes End Function請注意:不同的加密方式,密鑰長度有要求;密鑰可以寫入配置文件中,定期調整;
新聞熱點
疑難解答