函数生成随机8字符密码

时间:2015-04-28 09:03:42

标签: vb.net

我在vb中有这个功能,它似乎在生成8个字符的密码之前工作,但现在为每个注册到网站的用户生成相同的密码。我不确定我哪里出错了,因为我没有摆弄这段代码(代码不是我写的)?任何帮助将不胜感激。

function generatePassword()
  Dim i, newPassword
  newPassword = ""
  For i = 1 to 3
    newPassword = newPassword & Mid("ABCDEFGHJKLMNPQRSTUVWXYZ",randomInRange(1,24),1)
  Next
  For i = 4 to 7
    newPassword = newPassword & Mid("23456789",randomInRange(1,8),1)
  Next
  'For i = 1 to 8
  '  newPassword = newPassword & Mid("ABCDEFGHJKLMNPQRSTUVWXYZ23456789",randomInRange(1,32),1)
  'Next
  generatePassword = newPassword
end function

function randomInRange(lo,hi)
  randomInRange =(Int((hi - lo + 1) * rnd + lo))
end function

Dim newPassword = generatePassword()

            Dim strSql As String = "INSERT INTO Student(" & _
                             "StudentNo," & _
                             "Surname," & _
                             "FirstName," & _
                             "MiddleName," & _
                             "Gender," & _
                             "Pathway," & _
                             "[Level]," & _
                             "QubEmail," & _
                             "[Password]," & _
                             "HomeEmail," & _
                             "MobilePhone," & _
                             "HomeTown," & _
                             "PlacementYear," & _
                             "Status," & _
                             "DateEdited," & _
                             "HomePhone) " & _
                            "VALUES " & _
                            "( " & _
                             "'" & StudentNo.Text.Replace("'", "''") & "'," & _
                             "'" & Surname.Text.Replace("'", "''") & "'," & _
                             "'" & Forename.Text.Replace("'", "''") & "'," & _
                             "'" & MiddleName.Text.Replace("'", "''") & "'," & _
                            "'" & ddlGender.SelectedValue & "'," & _
                            "'" & ddlPathway.SelectedValue & "'," & _
                            "'" & ddlLevel.SelectedValue & "'," & _
                            "'" & QUBEmail.Text.Replace("'", "''") & "'," & _
                            "'" & newPassword & "'," & _
                             "'" & HomeEmail.Text.Replace("'", "''") & "'," & _
                            "'" & MobileNo.Text.Replace("'", "''") & "'," & _
                            "'" & HomeTown.Text.Replace("'", "''") & "'," & _
                            "" & PlacementYear & "," & _
                            "'Seeking Placement'," & _
                            " GETDATE() ," & _
                             "'" & HomeNo.Text.Replace("'", "''") & "' " & _
                            ")"


            Dim addStudent As OleDbDataReader = Database.DoSQLReturnDataReader(strSql)
            addStudent.Close()

2 个答案:

答案 0 :(得分:1)

似乎没有任何代码更新rnd变量。每次拨打randomInRange以获取不同的密码时,都需要更新。

我建议您完全更改randomInRange以使其运行得更干净。

这是你需要做的:

Private rnd As Random = new Random()
Function randomInRange(lo As Integer, hi As Integer) As Integer
    Return rnd.Next(lo, hi + 1)
End Function

进行此更改会生成随机密码。

答案 1 :(得分:-1)

如果您自己不是程序员,那么您应该与编写代码的人交谈。然而,Rnd实际上并非随机,而只是使用种子来计算其值的软化数学方程。

此种子设置为程序运行的时间。没有任何参与,种子将保持不变,这意味着所有的random实际上将是相同的值;在每个generatePassword()使用Randomize

Function generatePassword()
    Randomize()

    /.../
End Function