代码演示VB.NETDES加密解析

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. '/  
  14. '/ DES加密偏移量,必须是>=8位长的字符串  
  15. '/  
  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. '/  
  26. '/ DES加密的私钥,必须是8位长的字符串  
  27. '/  
  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. '/  
  39. '/ 对字符串进行DES加密  
  40. '/  
  41. '/  name="sourceString">待加密的字符串 
  42. '/ 加密后的BASE64编码的字符串 
  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. '/  
  68. '/ 对DES加密后的字符串进行解密  
  69. '/  
  70. '/  name="encryptedString">待解密的字符串 
  71. '/ 解密后的字符串 
  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. '/  
  98. '/ 对文件内容进行DES加密  
  99. '/  
  100. '/  name="sourceFile">待加密的文件绝对路径 
  101. '/  name="destFile">加密后的文件保存的绝对路径 
  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. '/  
  131. '/ 对文件内容进行DES加密,加密后覆盖掉原来的文件  
  132. '/  
  133. '/  name="sourceFile">待加密的文件的绝对路径 
  134. Overloads Public Sub EncryptFile(sourceFile As String)  
  135. EncryptFile(sourceFile, sourceFile)  
  136. End Sub 'EncryptFile  
  137.  
  138. '/  
  139. '/ 对文件内容进行DES解密  
  140. '/  
  141. '/  name="sourceFile">待解密的文件绝对路径 
  142. '/  name="destFile">解密后的文件保存的绝对路径 
  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. '/  
  172. '/ 对文件内容进行DES解密,加密后覆盖掉原来的文件  
  173. '/  
  174. '/  name="sourceFile">待解密的文件的绝对路径 
  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) 

网站标题:代码演示VB.NETDES加密解析
分享地址:http://www.csdahua.cn/qtweb/news17/372767.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网