SHA1 = New SHA1CryptoServiceProvider
' Convert the original string to array of Bytes
bytValue = _
System.Text.Encoding.UTF8.GetBytes(TextToHash)
' Compute the Hash, returns an array of Bytes
bytHash = SHA1.ComputeHash(bytValue)
SHA1.Clear()
' Return a base 64 encoded string of the Hash value
Debug.WriteLine(Convert.ToBase64String(bytHash))
End Sub
你可以将一个字符串传入这个例程,以得到散列值。例如,你可以传入“Paul”这个字符串,调试窗口将显示下列字符:
以下内容为程序代码:
w2h6uYgMJt/nq5ZqihcBteAXwv8=
现在将传入的值改为“Pauly”,你可以看到如下输出:
以下内容为程序代码:
proywxJ0znMpGF5sbB18+7GSAsM=
正象你看到的,传入字符串的一处小小的改动就生成了一个完全不同的结果。这也是散列如此有效的原因————很难得到字符转换的模式或者从加密的字符串中得到原始字符串的内容。
使用MD5计算散列
一旦你学会了使用一个散列类,你已经基本上掌握了所有的散列类。下面的方法使用的就是MD5散列算法。我想你已经注意到代码基本上是相同的,除了CryptoServiceProvider类是不同的。
以下内容为程序代码:
Private Sub HashTextMD5(ByVal TextToHash As String)
Dim md5 As MD5CryptoServiceProvider
Dim bytValue() As Byte
Dim bytHash() As Byte
' Create New Crypto Service Provider Object
md5 = New MD5CryptoServiceProvider
' Convert the original string to array of Bytes
bytValue = System.Text.Encoding. _
UTF8.GetBytes(TextToHash)
' Compute the Hash, returns an array of Bytes
bytHash = md5.ComputeHash(bytValue)
md5.Clear()
' Return a base 64 encoded string of the Hash value
Debug.WriteLine(Convert.ToBase64String(bytHash))
End Sub
传入“Paul”,MD5算法输出的字符串如下:
以下内容为程序代码:
nVWBsHh1MKNctPioSyqyTQ==
| 对此文章发表了评论 |

