当前位置: 首页 > 图文教程 > .Net技术 > VB.NET > VB.NET DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串

VB.NET
大话“基于对象”与“面向对象”
VB 内存分配与流读写 代码
VB.NET语法基础
vb.net与c#.net区别
用VB.Net读写数据库
百万程序员的苦恼-选择VB.NET还是C#
VB.net 多线程实例
VB.NET也区会大小写
VB.NET 2005编写定时关机程序
VB.NET控件编程定制拦截击键动作
如何实现.net程序的进程注入
VB.NET 菜单设计初级入门
VB.NET中关于DataGrid颜色的自定义
VB网络编程中Winsock的使用
VB.net编程教程:编写文字加解密程序
VB.NET中快速访问注册表技巧
VB.NET:在VB.NET中串行化对象
VB.NET:在 VB.NET 编程中使用数组
VB.NET:VB.NET路在何方?
大话“基于对象”与“面向对象”

VB.NET 中的 VB.NET DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 109 ::
收藏到网摘: n/a

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
'----------------------------------------------
' * DES加密、解密类库,加密结果使用BASE64编码返回,支持加密和解密文件,支持UNICODE及中文
' * 作者: 三角猫/DeltaCat
' * 网址: http://www.zu14.cn
' * 转载务必保留此信息
' * ---------------------------------------------
'
Namespace ZU14 NotInheritable Public Class DES Private iv As String = "1234的yzo" Private key As String = "123在yzo" '/ <summary> '/ DES加密偏移量,必须是>=8位长的字符串 '/ </summary> Public Property IV() As String Get Return iv End Get Set iv = value End Set End Property '/ <summary> '/ DES加密的私钥,必须是8位长的字符串 '/ </summary> Public Property Key() As String Get Return key End Get Set key = value End Set End Property '/ <summary> '/ 对字符串进行DES加密 '/ </summary> '/ <param name="sourceString">待加密的字符串</param> '/ <returns>加密后的BASE64编码的字符串</returns> 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 '/ <summary> '/ 对DES加密后的字符串进行解密 '/ </summary> '/ <param name="encryptedString">待解密的字符串</param> '/ <returns>解密后的字符串</returns> 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 '/ <summary> '/ 对文件内容进行DES加密 '/ </summary> '/ <param name="sourceFile">待加密的文件绝对路径</param> '/ <param name="destFile">加密后的文件保存的绝对路径</param> 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 '/ <summary> '/ 对文件内容进行DES加密,加密后覆盖掉原来的文件 '/ </summary> '/ <param name="sourceFile">待加密的文件的绝对路径</param> Overloads Public Sub EncryptFile(sourceFile As String) EncryptFile(sourceFile, sourceFile) End Sub 'EncryptFile '/ <summary> '/ 对文件内容进行DES解密 '/ </summary> '/ <param name="sourceFile">待解密的文件绝对路径</param> '/ <param name="destFile">解密后的文件保存的绝对路径</param> 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 '/ <summary> '/ 对文件内容进行DES解密,加密后覆盖掉原来的文件 '/ </summary> '/ <param name="sourceFile">待解密的文件的绝对路径</param> Overloads Public Sub DecryptFile(sourceFile As String) DecryptFile(sourceFile, sourceFile) End Sub 'DecryptFile End Class 'DES
End Namespace 'ZU14

 

使用方法

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)