vb.net DES加密与解密

本文介绍了使用Visual Basic实现的DES加密和解密方法。通过指定密钥和初始向量(IV),可以对字符串进行加密,并将加密后的结果转换为Base64字符串。同样地,也提供了从Base64字符串解密回原始字符串的功能。注意不同加密方式对密钥长度的要求。

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.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()
        Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
    End Function
2、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) 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

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

压缩包 : MD5 skey8位加密(文件).zip 列表 MD5 skey8位加密(文件)/ MD5 skey8位加密(文件)/bin/ MD5 skey8位加密(文件)/Form1.Designer.vb MD5 skey8位加密(文件)/Form1.resx MD5 skey8位加密(文件)/Form1.vb MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj MD5 skey8位加密(文件)/MD5 skey8位加密(文件).vbproj.user MD5 skey8位加密(文件)/My Project/ MD5 skey8位加密(文件)/My Project/Application.Designer.vb MD5 skey8位加密(文件)/My Project/Application.myapp MD5 skey8位加密(文件)/My Project/AssemblyInfo.vb MD5 skey8位加密(文件)/My Project/Resources.Designer.vb MD5 skey8位加密(文件)/My Project/Resources.resx MD5 skey8位加密(文件)/My Project/Settings.Designer.vb MD5 skey8位加密(文件)/My Project/Settings.settings MD5 skey8位加密(文件)/obj/ MD5 skey8位加密(文件)/obj/Debug/ MD5 skey8位加密(文件)/obj/Debug/CoreCompileInputs.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferences.cache MD5 skey8位加密(文件)/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).exe MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).pdb MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).vbprojResolveAssemblyReference.cache MD5 skey8位加密(文件)/obj/Debug/MD5 skey8位加密(文件).xml MD5 skey8位加密(文件)/obj/Debug/TempPE/ MD5 skey8位加密(文件)/obj/Debug/TempPE/My Project.Resources.Designer.vb.dll MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.exe MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Form1.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.pdb MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.Resources.resources MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.FileListAbsolute.txt MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.vbproj.GenerateResource.Cache MD5 skey8位加密(文件)/obj/Debug/WindowsApplication1.xml MD5 skey8位加密(文件)/obj/Release/ MD5 skey8位加密(文件).sln MD5 skey8位加密(文件).v11.suo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值