700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vb.net DES加密与解密

vb.net DES加密与解密

时间:2023-03-11 14:48:07

相关推荐

vb.net DES加密与解密

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) ThenmyKey = Me.JMKeyEnd IfIf String.IsNullOrEmpty(myIV) ThenmyIV = Me.JMIvEnd IfDim 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.MemoryStreamDim 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()Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)End Function2、DES解密Public Function DecryptDes(ByVal SourceStr As String, Optional ByVal myKey As String = "", Optional ByVal myIV As String = "") As String '使用标准DES对称解密If String.IsNullOrEmpty(SourceStr) ThenReturn SourceStrEnd IfIf SourceStr = "" ThenReturn SourceStrEnd IfIf String.IsNullOrEmpty(myKey) ThenmyKey = Me.JMKeyEnd IfIf String.IsNullOrEmpty(myIV) ThenmyIV = Me.JMIvEnd IfDim 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 DecryptDesEnd Function

请注意: 不同的加密方式,密钥长教程度有要求; 密钥可以写入配置文件中,定期调整;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。