您好,登錄后才能下訂單哦!
這篇文章主要介紹了VB.NET如何實(shí)現(xiàn)DES加密,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
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" '/ <summary> '/ DES加密偏移量,必須是>=8位長(zhǎng)的字符串 '/ </summary> Public Property IV() As String Get Return iv End Get Set iv = value End Set End Property '/ <summary> '/ DES加密的私鑰,必須是8位長(zhǎng)的字符串 '/ </summary> Public Property Key() As String Get Return key End Get Set key = value End Set End Property '/ <summary> '/ 對(duì)字符串進(jìn)行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> '/ 對(duì)DES加密后的字符串進(jìn)行解密 '/ </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> '/ 對(duì)文件內(nèi)容進(jìn)行DES加密 '/ </summary> '/ <param name="sourceFile">待加密的文件絕對(duì)路徑</param> '/ <param name="destFile">加密后的文件保存的絕對(duì)路徑</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> '/ 對(duì)文件內(nèi)容進(jìn)行DES加密,加密后覆蓋掉原來的文件 '/ </summary> '/ <param name="sourceFile">待加密的文件的絕對(duì)路徑</param> Overloads Public Sub EncryptFile(sourceFile As String) EncryptFile(sourceFile, sourceFile) End Sub 'EncryptFile '/ <summary> '/ 對(duì)文件內(nèi)容進(jìn)行DES解密 '/ </summary> '/ <param name="sourceFile">待解密的文件絕對(duì)路徑</param> '/ <param name="destFile">解密后的文件保存的絕對(duì)路徑</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> '/ 對(duì)文件內(nèi)容進(jìn)行DES解密,加密后覆蓋掉原來的文件 '/ </summary> '/ <param name="sourceFile">待解密的文件的絕對(duì)路徑</param> 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)
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“VB.NET如何實(shí)現(xiàn)DES加密”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。