让我的密码生成器生成“复杂”密码

时间:2014-01-30 11:15:33

标签: vb.net passwords

我的一个脚本中有一个密码生成器,它生成2个密码并将它们写入数据库。

稍后将检索这些密码并将其设置为2个用户的Windows登录密码,这些密码将在虚拟机中自动创建。

我遇到一个问题,就Windows而言,我的密码并不总是被认为是复杂的。

我正在使用以下函数生成我的密码......

    Public Function GeneratePassword(ByVal len As Integer) As String
    Dim str As String = "1234567890qwertyuio1pa2sd3fg4hj5kl6zx7cv8bn9mQ0WE1RT2YU3IO4PA5SD6FG7HJ8KL9ZX0CVBNM"
    Dim N As Integer = str.Length
    Dim rnd As New Random((Now.Hour * 3600 + Now.Minute * 60 +
    Now.Second) * 1000 + Now.Millisecond)
    Dim sb As New StringBuilder
    For l As Integer = 1 To len
    sb.Append(str.Substring(rnd.Next(0, N), 1))
    Next
    Return sb.ToString
    End Function

然后像这样调用......

    Dim vm_password1 As String = GeneratePassword(10)

如何最好地修改此代码以确保生成的每个密码包含1个大写字符,1个小写字符和1个数字?

任何帮助表示赞赏! :)

1 个答案:

答案 0 :(得分:0)

当然你可以进一步扩展这个,但这里是jist

Public Function fnGenPw() As String

    Dim RandomClass As New Random()
    Const legalAlpha As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Const legalNums As String = "1234567890"

    Dim strbNewpw As New StringBuilder

    For i As Integer = 0 To 2 'first 3 characters are alpha
        Dim RandomNumber As Integer = RandomClass.Next(legalAlpha.Length - 1)
        strbNewpw.Append(legalAlpha.Chars(RandomNumber))
    Next
    For i As Integer = 3 To 5 'characters 4,5,6 are numeric
        Dim RandomNumber As Integer = RandomClass.Next(legalNums.Length - 1)
        strbNewpw.Append(legalNums.Chars(RandomNumber))
    Next
    For i As Integer = 6 To 9 'characters 7,8,9,10 are alpha
        Dim RandomNumber As Integer = RandomClass.Next(legalAlpha.Length - 1)
        strbNewpw.Append(legalAlpha.Chars(RandomNumber))
    Next

    Return strbNewpw.ToString
End Function