我正在创建一个应用程序,我需要一点帮助。这只是一个简单的密码生成器。我让应用程序生成密码,没有问题,但我需要添加一个步骤,检查:1个大写字母,1个小写字母,1个数字和1个特殊字符,然后显示密码。如果密码不包含这些值,则应该再次生成密码。
我想保留我已有的代码,我只想在最后添加一个步骤。
在此之前非常感谢。
下面是我的代码:
Public Class Form1
Dim AllCharacters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?!£$%^&*()_+[];'#,./?><~@:}{\|"
Dim r As New Random
Dim charIndex As Integer
Dim finalpassword As String
Dim passwordChars1() As Char = New Char(9) {}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To 9 - 1
charIndex = r.Next(AllCharacters.Length)
passwordChars1(i) = AllCharacters(charIndex)
Next
finalpassword = passwordChars1
passwordbox.Text = finalpassword 'Displays Password on Main Form Window
End Sub
发布于 2018-05-24 05:22:44
这是在创建或更改密码时通常要做的事情。我通常使用类似以下的函数来验证密码的复杂性:
'PasswordComplexityRegex is a string value I get from a database at login,
'if it is not present then a default one will be use.
Dim PasswordComplexityRegex as String
'MinimunPasswordLenght is also a number stored in a database if not present
'it will use a default, in this case 12.
Public Function validatePassword(ByVal pass As String) As Boolean
Dim pattern As String = "[~`!@#$%^&*()-_=+';:,./<>?]"
If Not (String.IsNullOrEmpty(PasswordComplexityRegex)) Then
pattern = PasswordComplexityRegex
End If
Dim patternRegex As Match = Regex.Match(pass, pattern)
'In this case it checks that the length of the password is >= to 12
If (IsDBNull(MinimunPasswordLenght) Or MinimunPasswordLenght = 0) Then
MinimunPasswordLenght = 12
End If
Return (patternRegex.Success And pass.Length >= MinimunPasswordLenght)
End Function
关键是你总是测试你的正则表达式,它比一些人想象的要复杂一点。
现在使用此函数来验证您的密码,并确定是否继续。类似于:
If Not validatePassword(txtNewPassword.Text) Then
MsgBox("Password need to be...", vbCritical, "Password not in compliance...")
End If
您应该允许包括Unicode在内的所有字符,并且不应该限制超过严格必要的长度,例如,如果MySQL限制长度。你应该鼓励字母、数字、大写等的组合。
Nandostyle
https://stackoverflow.com/questions/50468207
复制相似问题