| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 加密压缩编程 >> 实例分析正文
最新推荐 更多内容
SHA256 Hash加密的实例
SHA256 Hash加密的实例
作者:Ilu    来源:乐博网整理     更新时间:2009-9-3
SHA256 Hash加密的代码如下:
  1. #Region " SHA256 Hash "
  2.     Public Class SHA256ManagedHash
  3.  
  4. #Region " Global Variables "
  5.         'The encoding to get the byte array from the given data.
  6.         Private Shared _encoding As New UTF8Encoding
  7.         'The hash algorithm.
  8.         Private Shared hashAlg As New SHA256Managed
  9. #End Region
  10.         'Please note that I'm using Overloads.
  11.         'I have chosen to do it this way since it's clearer
  12.         'and it's more "flexible".
  13. #Region " SHA256 Hash Events "
  14.         'I'm using ByRef salt as Byte(), so that the orignal variable will hold the
  15.         'generated salt, so that it can be saved, edited, etc.
  16.         Public Overloads Shared Function SHA256StringHash(ByVal text As String, ByRef salt As Byte()) As String
  17.             Dim textBytes As Byte() = _encoding.GetBytes(text)
  18.             Dim myrandom As New Random
  19.             Dim rng As New RNGCryptoServiceProvider
  20.  
  21.             salt = New Byte(myrandom.Next(4, 8)) {}
  22.             rng.GetNonZeroBytes(salt)
  23.             Dim SaltedText_Byte As Byte() = New Byte((textBytes.Length + salt.Length) - 1) {}
  24.             SaltedText_Byte = textBytes.Concat(salt).ToArray
  25.             'It "returns" to the third overload (actually sends, but whatever :P)
  26.             'the generated byte array,
  27.             'which holds the text and the salt's byte array.
  28.             Return SHA256StringHash(SaltedText_Byte)
  29.         End Function
  30.  
  31.         Public Overloads Shared Function SHA256StringHash(ByVal text As String) As String
  32.             'Get the text's byte array and sends it to the third overload.
  33.             Dim textBytes As Byte() = _encoding.GetBytes(text)
  34.             Return SHA256StringHash(textBytes)
  35.         End Function
  36.  
  37.         Public Overloads Shared Function SHA256StringHash(ByVal data As Byte()) As String
  38.             'Compute the hash from the received byte array and returns it
  39.             'to the calling method.
  40.             Dim hashedData As Byte() = hashAlg.ComputeHash(data)
  41.             Return Convert.ToBase64String(hashedData)
  42.         End Function
  43. #End Region
  44.  
  45. #Region " Verify Hash "
  46.  
  47.         Public Overloads Shared Function VerifyHash(ByVal text As String, ByVal hash As String, ByVal salt As Byte()) As Boolean
  48.             Dim textBytes As Byte() = _encoding.GetBytes(text)
  49.             Dim hashByte As Byte() = Convert.FromBase64String(hash)
  50.  
  51.             Return VerifyHash(textBytes, hashByte, salt)
  52.         End Function
  53.  
  54.         Public Overloads Shared Function VerifyHash(ByVal data As Byte(), ByVal hash As String, ByVal salt As Byte()) As Boolean
  55.             Dim hashBytes As Byte() = _encoding.GetBytes(hash)
  56.  
  57.             Return VerifyHash(data, hashBytes, salt)
  58.         End Function
  59.  
  60.         Public Overloads Shared Function VerifyHash(ByVal text As String, ByVal hash As String, ByVal salt As String) As Boolean
  61.             Dim textBytes As Byte() = _encoding.GetBytes(text)
  62.             Dim hashBytes As Byte() = _encoding.GetBytes(hash)
  63.             Dim saltBytes As Byte() = _encoding.GetBytes(salt)
  64.  
  65.             Return VerifyHash(textBytes, hashBytes, saltBytes)
  66.         End Function
  67.  
  68.         Public Overloads Shared Function VerifyHash(ByVal data As Byte(), ByVal hash As Byte(), ByVal salt As Byte()) As Boolean
  69.             Dim hashCheck((data.Length + salt.Length) - 1) As Byte
  70.             hashCheck = data.Concat(salt).ToArray
  71.             Dim verify As Byte() = hashAlg.ComputeHash(hashCheck)
  72.             Dim IsMatch As Boolean = False
  73.             For i As Integer = 0 To verify.Length - 1
  74.                 If verify(i) = hash(i) Then
  75.                     IsMatch = True
  76.                 Else
  77.                     IsMatch = False
  78.                 End If
  79.             Next
  80.             Return IsMatch
  81.         End Function
  82. #End Region
  83.     End Class
  84. #End Region
  85.  
  86. End Namespace
  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net获取随机密码的实例
    VB.Net用Salt哈希数据的实例
    用DPAPI加密和解密数据的实例
    储存加密的数据库连接字符串到注册表的实例
    Salt加密数据的.Net实例
    Rijndael加密解密的实例
    AES加密的实例
    Rijndael加密的实例
    VB.Net对XML元素进行加密解密的实例
    VB.Net文件CRC32算法的实例
    VB.Net文件SHA1算法的实例
    Blowfish加密算法的实例
    Rijndael类的实例
    TripleDES加解密的实例
    VB.Net实现des加密算法的实例
    VB.Net实现RSA加密的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]