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

开发 后端
大家还为VB.NET DES加密烦恼吗?在这里给大家举了一个详细的例子,代码清晰,希望大家看过会有技术上的提高。

VB.NET经过长时间的发展,很多用户都很了解VB.NET了,这里我发表一下个人理解,和大家讨论关于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运行环境
责任编辑:田树 来源: 博客
相关推荐

2009-11-03 11:06:40

VB.NET事件

2009-11-02 09:45:23

VB.NET文件系统对

2010-01-14 14:56:07

2009-10-28 15:18:46

VB.NET网络应用

2009-10-27 10:58:00

VB.NET文件名排序

2009-10-26 10:30:57

VB.NET处理FTP

2009-10-26 14:50:18

VB.NET遍历注册表

2009-10-26 09:50:20

VB.NET Star

2010-01-14 13:08:37

VB.NET运算符

2009-10-09 15:59:41

VB.NET对象

2009-10-27 14:05:59

VB.NET程序

2009-10-26 11:04:36

VB.NET UDP协

2009-10-23 14:31:05

VB.NET类定义

2010-01-18 16:33:57

VB.NET加密文件

2009-10-14 10:08:05

VB.NET编写DEC

2009-10-14 09:29:43

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
点赞
收藏

51CTO技术栈公众号