代码演示VB.NET DES加密解析
创始人
2024-06-10 04:41:48
0

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.  
  18. Public Property IV() As String  
  19. Get  
  20. Return iv  
  21. End Get  
  22. Set  
  23. iv = value 
  24. End Set  
  25. End Property  
  26. '/  
  27. '/ DES加密的私钥,必须是8位长的字符串  
  28. '/ 
  29.  
  30.  
  31. Public Property Key() As String  
  32. Get  
  33. Return key  
  34. End Get  
  35. Set  
  36. key = value 
  37. End Set  
  38. End Property  
  39.  
  40. '/  
  41. '/ 对字符串进行DES加密  
  42. '/ 
  43.  
  44. '/  name="sourceString">待加密的字符串 
  45. '/ 加密后的BASE64编码的字符串 
  46. Public Function Encrypt(sourceString As String) As String  
  47. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  48. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  49. Dim des As New DESCryptoServiceProvider()  
  50. Dim ms As New MemoryStream()  
  51. Try  
  52. Dim inData As Byte() = Encoding.Default.GetBytes(sourceString)  
  53. Try  
  54. Dim cs As New CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)  
  55. Try  
  56. cs.Write(inData, 0, inData.Length)  
  57. cs.FlushFinalBlock()  
  58. Finally  
  59. cs.Dispose()  
  60. End Try  
  61.  
  62. Return Convert.ToBase64String(ms.ToArray())  
  63. Catch  
  64. End Try  
  65. Finally  
  66. ms.Dispose()  
  67. End Try  
  68. End Function 'Encrypt  
  69.  
  70. '/  
  71. '/ 对DES加密后的字符串进行解密  
  72. '/ 
  73.  
  74. '/  name="encryptedString">待解密的字符串 
  75. '/ 解密后的字符串 
  76. Public Function Decrypt(encryptedString As String) As String  
  77. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  78. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  79. Dim des As New DESCryptoServiceProvider()  
  80.  
  81. Dim ms As New MemoryStream()  
  82. Try  
  83. Dim inData As Byte() = Convert.FromBase64String(encryptedString)  
  84. Try  
  85. Dim cs As New CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)  
  86. Try  
  87. cs.Write(inData, 0, inData.Length)  
  88. cs.FlushFinalBlock()  
  89. Finally  
  90. cs.Dispose()  
  91. End Try  
  92.  
  93. Return Encoding.Default.GetString(ms.ToArray())  
  94. Catch  
  95. End Try  
  96. Finally  
  97. ms.Dispose()  
  98. End Try  
  99. End Function 'Decrypt  
  100.  
  101. '/  
  102. '/ 对文件内容进行DES加密  
  103. '/ 
  104.  
  105. '/  name="sourceFile">待加密的文件绝对路径 
  106. '/  name="destFile">加密后的文件保存的绝对路径 
  107. Overloads Public Sub EncryptFile(sourceFile As String, destFile As String)  
  108. If Not File.Exists(sourceFile) Then  
  109. Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)  
  110. End If  
  111. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  112. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  113. Dim des As New DESCryptoServiceProvider()  
  114. Dim btFile As Byte() = File.ReadAllBytes(sourceFile)  
  115.  
  116. Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)  
  117. Try  
  118. Try  
  119. Dim cs As New CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)  
  120. Try  
  121. cs.Write(btFile, 0, btFile.Length)  
  122. cs.FlushFinalBlock()  
  123. Finally  
  124. cs.Dispose()  
  125. End Try  
  126. Catch  
  127. Finally  
  128. fs.Close()  
  129. End Try  
  130. Finally  
  131. fs.Dispose()  
  132. End Try  
  133. End Sub 'EncryptFile  
  134.  
  135. '/  
  136. '/ 对文件内容进行DES加密,加密后覆盖掉原来的文件  
  137. '/ 
  138.  
  139. '/  name="sourceFile">待加密的文件的绝对路径 
  140. Overloads Public Sub EncryptFile(sourceFile As String)  
  141. EncryptFile(sourceFile, sourceFile)  
  142. End Sub 'EncryptFile  
  143.  
  144. '/  
  145. '/ 对文件内容进行DES解密  
  146. '/ 
  147.  
  148. '/  name="sourceFile">待解密的文件绝对路径 
  149. '/  name="destFile">解密后的文件保存的绝对路径 
  150. Overloads Public Sub DecryptFile(sourceFile As String, destFile As String)  
  151. If Not File.Exists(sourceFile) Then  
  152. Throw New FileNotFoundException("指定的文件路径不存在!", sourceFile)  
  153. End If  
  154. Dim btKey As Byte() = Encoding.Default.GetBytes(key)  
  155. Dim btIV As Byte() = Encoding.Default.GetBytes(iv)  
  156. Dim des As New DESCryptoServiceProvider()  
  157. Dim btFile As Byte() = File.ReadAllBytes(sourceFile)  
  158.  
  159. Dim fs As New FileStream(destFile, FileMode.Create, FileAccess.Write)  
  160. Try  
  161. Try  
  162. Dim cs As New CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)  
  163. Try  
  164. cs.Write(btFile, 0, btFile.Length)  
  165. cs.FlushFinalBlock()  
  166. Finally  
  167. cs.Dispose()  
  168. End Try  
  169. Catch  
  170. Finally  
  171. fs.Close()  
  172. End Try  
  173. Finally  
  174. fs.Dispose()  
  175. End Try  
  176. End Sub 'DecryptFile  
  177.  
  178. '/  
  179. '/ 对文件内容进行DES解密,加密后覆盖掉原来的文件  
  180. '/ 
  181.  
  182. '/  name="sourceFile">待解密的文件的绝对路径 
  183. Overloads Public Sub DecryptFile(sourceFile As String)  
  184. DecryptFile(sourceFile, sourceFile)  
  185. End Sub 'DecryptFile  
  186. End Class 'DES  
  187. 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运行环境

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...