189 8069 5689

代码演示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加密解析
本文URL:http://cdxtjz.cn/article/codgigo.html

联系我们

您好HELLO!
感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

小谭建站工作室

成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

小谭观点

相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。