VB.NET 的 DES 加密与解密(转载于nofort博客)

MD5和SHA的加密方式都是单向的,就算是我写的程序用户的密码用这两种方式保存后,我对着看也不知道他们的密码是什么,这用在保存密码上是不错的,但如果是一些需要能将密文还原的应用来说就不合适了,所以接下来就是我们的DES算法上场了。
DES要求两个关健数据 一个是key(密钥)一个是初始化向量(IV)只要这两个数据一致就可以还原出原来的数据,为了方便还是写成函数
TripleDES算法基本上DES是一样的不过加密的强度不同,同SHA1和SHA512区别一样,更高的强度,意味着运算速度的相应下降。
其实用这种方法加密最重要的是保护好KEY和IV。

 加密函数

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

解密函数

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

调用

Dim mytxt, EDes, msgtxt As String
        mytxt = "nyfort的BLOG"
        '加密
        '注意KEY和IV只能用英文和数字,des是8个字符
        EDes = EncryptDes(mytxt, "12345678", "abcdefgh")
        msgtxt = "nyfort的BLOG" & vbCrLf & "SHE加密" & vbCrLf & EDes
        MessageBox.Show(msgtxt, "SHE加密")
        '解密
        mytxt = DecryptDes(EDes, "12345678", "abcdefgh")
        msgtxt = EDes & vbCrLf & "SHE解密" & vbCrLf & mytxt
        MessageBox.Show(msgtxt, "SHE解密")


压缩包 : 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、付费专栏及课程。

余额充值