VB.NET经过长时间的发展,很多用户都很了解VB.NET了,这里我发表一下个人理解,和大家讨论关于VB.NET DES加密的事,需要VB.NET的,就把C#的转换了一下,欢迎多交流。
VB.NET DES加密代码:
- Imports System
- Imports System.Collections.Generic
- Imports System.Text
- Imports System.IO
- Imports System.Security
- Imports System.Security.Cryptography
- Namespace ZU14
- NotInheritable Public Class DES
- Private iv As String = "1234的yzo"
- Private key As String = "123在yzo"
- '/
- '/ DES加密偏移量,必须是>=8位长的字符串
- '/
- Public Property IV() As String
- Get
- Return iv
- End Get
- Set
- iv = value
- End Set
- End Property
- '/
- '/ DES加密的私钥,必须是8位长的字符串
- '/
- Public Property Key() As String
- Get
- Return key
- End Get
- Set
- key = value
- End Set
- End Property
- '/
- '/ 对字符串进行DES加密
- '/
- '/ name="sourceString">待加密的字符串
- '/
加密后的BASE64编码的字符串 - Public Function Encrypt(sourceString As String) As String
- Dim btKey As Byte() = Encoding.Default.GetBytes(key)
- Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
- Dim des As New DESCryptoServiceProvider()
- Dim ms As New MemoryStream()
- Try
- Dim inData As Byte() = Encoding.Default.GetBytes(sourceString)
- Try
- Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
- Try
- cs.Write(inData, 0, inData.Length)
- cs.FlushFinalBlock()
- Finally
- cs.Dispose()
- End Try
- Return Convert.ToBase64String(ms.ToArray())
- Catch
- End Try
- Finally
- ms.Dispose()
- End Try
- End Function 'Encrypt
- '/
- '/ 对DES加密后的字符串进行解密
- '/
- '/ name="encryptedString">待解密的字符串
- '/
解密后的字符串 - Public Function Decrypt(encryptedString As String) As String
- Dim btKey As Byte() = Encoding.Default.GetBytes(key)
- Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
- Dim des As New DESCryptoServiceProvider()
- Dim ms As New MemoryStream()
- Try
- Dim inData As Byte() = Convert.FromBase64String(encryptedString)
- Try
- Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
- Try
- cs.Write(inData, 0, inData.Length)
- cs.FlushFinalBlock()
- Finally
- cs.Dispose()
- End Try
- Return Encoding.Default.GetString(ms.ToArray())
- Catch
- End Try
- Finally
- ms.Dispose()
- End Try
- End Function 'Decrypt
- '/
- '/ 对文件内容进行DES加密
- '/
- '/ name="sourceFile">待加密的文件绝对路径
- '/ name="destFile">加密后的文件保存的绝对路径
- Overloads Public Sub EncryptFile(sourceFile As String, destFile As String)
- If Not File.Exists(sourceFile) Then
- Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
- End If
- Dim btKey As Byte() = Encoding.Default.GetBytes(key)
- Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
- Dim des As New DESCryptoServiceProvider()
- Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
- Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
- Try
- Try
- Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)
- Try
- cs.Write(btFile, 0, btFile.Length)
- cs.FlushFinalBlock()
- Finally
- cs.Dispose()
- End Try
- Catch
- Finally
- fs.Close()
- End Try
- Finally
- fs.Dispose()
- End Try
- End Sub 'EncryptFile
- '/
- '/ 对文件内容进行DES加密,加密后覆盖掉原来的文件
- '/
- '/ name="sourceFile">待加密的文件的绝对路径
- Overloads Public Sub EncryptFile(sourceFile As String)
- EncryptFile(sourceFile, sourceFile)
- End Sub 'EncryptFile
- '/
- '/ 对文件内容进行DES解密
- '/
- '/ name="sourceFile">待解密的文件绝对路径
- '/ name="destFile">解密后的文件保存的绝对路径
- Overloads Public Sub DecryptFile(sourceFile As String, destFile As String)
- If Not File.Exists(sourceFile) Then
- Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)
- End If
- Dim btKey As Byte() = Encoding.Default.GetBytes(key)
- Dim btIV As Byte() = Encoding.Default.GetBytes(iv)
- Dim des As New DESCryptoServiceProvider()
- Dim btFile As Byte() = File.ReadAllBytes(sourceFile)
- Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)
- Try
- Try
- Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)
- Try
- cs.Write(btFile, 0, btFile.Length)
- cs.FlushFinalBlock()
- Finally
- cs.Dispose()
- End Try
- Catch
- Finally
- fs.Close()
- End Try
- Finally
- fs.Dispose()
- End Try
- End Sub 'DecryptFile
- '/
- '/ 对文件内容进行DES解密,加密后覆盖掉原来的文件
- '/
- '/ name="sourceFile">待解密的文件的绝对路径
- Overloads Public Sub DecryptFile(sourceFile As String)
- DecryptFile(sourceFile, sourceFile)
- End Sub 'DecryptFile
- End Class 'DES
- End Namespace 'ZU14
VB.NET DES加密使用方法:
- Dim des As New ZU14.DES()
- des.IV = "abcd哈哈笑"
- des.Key = "必须八位"
- Dim es As String = des.Encrypt("在")
- Console.WriteLine(es)
- Console.Write(des.Decrypt(es))
- des.EncryptFile("d:\a.txt", "d:\b.txt")
- des.DecryptFile("d:\b.txt")
- Console.ReadKey(True)
网站标题:代码演示VB.NETDES加密解析
分享地址:http://www.csdahua.cn/qtweb/news17/372767.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网