自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

代碼演示VB.NET DES加密解析

開發(fā) 后端
大家還為VB.NET DES加密煩惱嗎?在這里給大家舉了一個詳細的例子,代碼清晰,希望大家看過會有技術上的提高。

VB.NET經過長時間的發(fā)展,很多用戶都很了解VB.NET了,這里我發(fā)表一下個人理解,和大家討論關于VB.NET DES加密的事,需要VB.NET的,就把C#的轉換了一下,歡迎多交流。

VB.NET DES加密代碼:

  1. Imports System  
  2. Imports System.Collections.Generic  
  3. Imports System.Text  
  4. Imports System.IO  
  5. Imports System.Security  
  6. Imports System.Security.Cryptography  
  7.  
  8. Namespace ZU14  
  9. NotInheritable Public Class DES  
  10. Private iv As String = "1234的yzo" 
  11. Private key As String = "123在yzo" 
  12.  
  13. '/ <summary> 
  14. '/ DES加密偏移量,必須是>=8位長的字符串  
  15. '/ </summary> 
  16.  
  17. Public Property IV() As String  
  18. Get  
  19. Return iv  
  20. End Get  
  21. Set  
  22. iv = value 
  23. End Set  
  24. End Property  
  25. '/ <summary> 
  26. '/ DES加密的私鑰,必須是8位長的字符串  
  27. '/ </summary> 
  28.  
  29. Public Property Key() As String  
  30. Get  
  31. Return key  
  32. End Get  
  33. Set  
  34. key = value 
  35. End Set  
  36. End Property  
  37.  
  38. '/ <summary> 
  39. '/ 對字符串進行DES加密  
  40. '/ </summary> 
  41. '/ <param name="sourceString">待加密的字符串</param> 
  42. '/ <returns>加密后的BASE64編碼的字符串</returns> 
  43. Public Function Encrypt(sourceString As String) As String  
  44. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  45. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  46. Dim des As New DESCryptoServiceProvider()  
  47. Dim ms As New MemoryStream()  
  48. Try  
  49. Dim inData As Byte() = Encoding.Default.GetBytes(sourceString)  
  50. Try  
  51. Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)  
  52. Try  
  53. cs.Write(inData, 0, inData.Length)  
  54. cs.FlushFinalBlock()  
  55. Finally  
  56. cs.Dispose()  
  57. End Try  
  58.  
  59. Return Convert.ToBase64String(ms.ToArray())  
  60. Catch  
  61. End Try  
  62. Finally  
  63. ms.Dispose()  
  64. End Try  
  65. End Function 'Encrypt  
  66.  
  67. '/ <summary> 
  68. '/ 對DES加密后的字符串進行解密  
  69. '/ </summary> 
  70. '/ <param name="encryptedString">待解密的字符串</param> 
  71. '/ <returns>解密后的字符串</returns> 
  72. Public Function Decrypt(encryptedString As String) As String  
  73. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  74. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  75. Dim des As New DESCryptoServiceProvider()  
  76.  
  77. Dim ms As New MemoryStream()  
  78. Try  
  79. Dim inData As Byte() = Convert.FromBase64String(encryptedString)  
  80. Try  
  81. Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)  
  82. Try  
  83. cs.Write(inData, 0, inData.Length)  
  84. cs.FlushFinalBlock()  
  85. Finally  
  86. cs.Dispose()  
  87. End Try  
  88.  
  89. Return Encoding.Default.GetString(ms.ToArray())  
  90. Catch  
  91. End Try  
  92. Finally  
  93. ms.Dispose()  
  94. End Try  
  95. End Function 'Decrypt  
  96.  
  97. '/ <summary> 
  98. '/ 對文件內容進行DES加密  
  99. '/ </summary> 
  100. '/ <param name="sourceFile">待加密的文件絕對路徑</param> 
  101. '/ <param name="destFile">加密后的文件保存的絕對路徑</param> 
  102. Overloads Public Sub EncryptFile(sourceFile As String, destFile As String)  
  103. If Not File.Exists(sourceFile) Then  
  104. Throw New FileNotFoundException("指定的文件路徑不存在!", sourceFile)  
  105. End If  
  106. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  107. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  108. Dim des As New DESCryptoServiceProvider()  
  109. Dim btFile As Byte() = File.ReadAllBytes(sourceFile)  
  110.  
  111. Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)  
  112. Try  
  113. Try  
  114. Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)  
  115. Try  
  116. cs.Write(btFile, 0, btFile.Length)  
  117. cs.FlushFinalBlock()  
  118. Finally  
  119. cs.Dispose()  
  120. End Try  
  121. Catch  
  122. Finally  
  123. fs.Close()  
  124. End Try  
  125. Finally  
  126. fs.Dispose()  
  127. End Try  
  128. End Sub 'EncryptFile  
  129.  
  130. '/ <summary> 
  131. '/ 對文件內容進行DES加密,加密后覆蓋掉原來的文件  
  132. '/ </summary> 
  133. '/ <param name="sourceFile">待加密的文件的絕對路徑</param> 
  134. Overloads Public Sub EncryptFile(sourceFile As String)  
  135. EncryptFile(sourceFile, sourceFile)  
  136. End Sub 'EncryptFile  
  137.  
  138. '/ <summary> 
  139. '/ 對文件內容進行DES解密  
  140. '/ </summary> 
  141. '/ <param name="sourceFile">待解密的文件絕對路徑</param> 
  142. '/ <param name="destFile">解密后的文件保存的絕對路徑</param> 
  143. Overloads Public Sub DecryptFile(sourceFile As String, destFile As String)  
  144. If Not File.Exists(sourceFile) Then  
  145. Throw New FileNotFoundException("指定的文件路徑不存在!", sourceFile)  
  146. End If  
  147. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  148. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  149. Dim des As New DESCryptoServiceProvider()  
  150. Dim btFile As Byte() = File.ReadAllBytes(sourceFile)  
  151.  
  152. Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)  
  153. Try  
  154. Try  
  155. Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)  
  156. Try  
  157. cs.Write(btFile, 0, btFile.Length)  
  158. cs.FlushFinalBlock()  
  159. Finally  
  160. cs.Dispose()  
  161. End Try  
  162. Catch  
  163. Finally  
  164. fs.Close()  
  165. End Try  
  166. Finally  
  167. fs.Dispose()  
  168. End Try  
  169. End Sub 'DecryptFile  
  170.  
  171. '/ <summary> 
  172. '/ 對文件內容進行DES解密,加密后覆蓋掉原來的文件  
  173. '/ </summary> 
  174. '/ <param name="sourceFile">待解密的文件的絕對路徑</param> 
  175. Overloads Public Sub DecryptFile(sourceFile As String)  
  176. DecryptFile(sourceFile, sourceFile)  
  177. End Sub 'DecryptFile  
  178. End Class 'DES  
  179. End Namespace 'ZU14 

VB.NET DES加密使用方法:

  1. Dim des As New ZU14.DES()  
  2. des.IV = "abcd哈哈笑" 
  3. des.Key = "必須八位" 
  4.  
  5. Dim es As String = des.Encrypt("在")  
  6. Console.WriteLine(es)  
  7. Console.Write(des.Decrypt(es))  
  8.  
  9. des.EncryptFile("d:\a.txt", "d:\b.txt")  
  10. des.DecryptFile("d:\b.txt")   
  11.  
  12. Console.ReadKey(True) 

【編輯推薦】

  1. VB.NET重命名批量修改大揭秘
  2. 程序員必看VB.NET CASE語句拓展篇
  3. 深入介紹VB.NET類庫 SmartRWLocker技巧
  4. VB.NET復制讀取音頻文件到剪貼板小技巧
  5. 深入概括VB.NET運行環(huán)境
責任編輯:田樹 來源: 博客
相關推薦

2009-11-03 11:06:40

VB.NET事件

2009-11-02 09:45:23

VB.NET文件系統(tǒng)對

2010-01-14 14:56:07

2009-10-27 10:58:00

VB.NET文件名排序

2009-10-28 15:18:46

VB.NET網絡應用

2009-10-26 09:50:20

VB.NET Star

2009-10-09 15:59:41

VB.NET對象

2009-10-26 10:30:57

VB.NET處理FTP

2009-10-26 14:50:18

VB.NET遍歷注冊表

2010-01-14 13:08:37

VB.NET運算符

2009-10-23 14:31:05

VB.NET類定義

2009-10-26 11:04:36

VB.NET UDP協(xié)

2009-10-27 14:05:59

VB.NET程序

2010-01-14 17:41:57

VB.NET變量范圍

2010-01-21 16:37:56

VB.NET變量聲明

2010-01-15 16:46:05

VB.NET集合存儲

2009-11-02 14:48:45

VB.NET HOOK

2010-01-18 16:33:57

VB.NET加密文件

2009-10-14 09:29:43

VB.NET加密

2009-10-14 10:08:05

VB.NET編寫DEC
點贊
收藏

51CTO技術棧公眾號