0

我正在尝试对我的网站的密码进行哈希处理,并且我已经进行了一些试验,并且得到了结果。现在我在问自己这是否真的是一种散列密码的好方法。

我的主要代码:

Imports System.Security.Cryptography

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim strWoordOmTeHashen As String
    Dim strSalt1, strSalt2, strSalt3 As String
    Dim random As New Random

    Dim arrSalt1(255), arrSalt2(255), arrSalt3(255) As String

    For i = 0 To 255
        arrSalt1(i) = random.Next(1, 26).ToString
        arrSalt2(i) = random.Next(1, 26).ToString
        arrSalt3(i) = random.Next(1, 26).ToString
    Next

    For i = 0 To 255
        arrSalt1(i) = VeranderGetalNaarLetter.VeranderGetalNaarLetter(CInt(arrSalt1(i)))
        arrSalt2(i) = VeranderGetalNaarLetter.VeranderGetalNaarLetter(CInt(arrSalt2(i)))
        arrSalt3(i) = VeranderGetalNaarLetter.VeranderGetalNaarLetter(CInt(arrSalt3(i)))
    Next

    For i = 0 To 255
        strSalt1 &= arrSalt1(i)
        strSalt2 &= arrSalt2(i)
        strSalt3 &= arrSalt3(i)
    Next


    strWoordOmTeHashen = strSalt1 & strSalt2 & txtWoord.Text & strSalt3

    'Sha512 zoder salt
    Dim sham As New SHA512Managed
    Dim result As Byte()
    Dim data As Byte()
    Dim hexstring As String

    data = ASCIIEncoding.ASCII.GetBytes(strWoordOmTeHashen)
    result = sham.ComputeHash(data)

    For i = 0 To UBound(result)
        hexstring &= Hex(result(i)).ToLower
    Next

    TextBox1.Text = hexstring

End Sub
End Class

你可能会注意到我正在调用一个函数。我正在调用这个函数:Public Class VeranderGetalNaarLetter

Public Shared Function VeranderGetalNaarLetter(intSalt As Integer) As String

    Dim strAlfabet As String = "!abcdefghijklmnopqrstuvwxyz"
    Dim strLetter As String

    strLetter = strAlfabet.Substring(intSalt, 1)


    Return strLetter
End Function


End Class

欢迎任何评论。我希望得到评论来改进我的编程。提前致谢 :)

4

2 回答 2

1

虽然这没有错,但也不是最佳实践。散列密码非常乏味,有时重新发明轮子是不值得的。如果您需要对密码进行哈希处理,那么您应该使用已经存在的库。请看一下 BCrypt http://bcrypt.codeplex.com/

于 2013-04-28T23:54:33.840 回答
0

这是我通常用来散列内容的代码,实际上它是一个非常简单的函数:)

Function hash(text As String) As String
    Dim encoder As New System.Text.UnicodeEncoding
    Dim sha256 As New System.Security.Cryptography.SHA256CryptoServiceProvider
    Return Convert.ToBase64String(sha256.ComputeHash(encoder.GetBytes(text)))
End Function

祝你好运!

于 2013-04-28T18:04:08.297 回答