0

Need a little help with an application I'm creating. Its just a simple password generator. I have the application generating the password with no issues but I need to add a step in that checks for: 1 uppercase letter, 1 lowercase letter, 1 number and 1 special character before displaying the password. If the password doesn't contain these values the password should then generate again.

I would like to keep the code I have, i just want to add a step in at the end.

Thanks a lot in advance.

Here is my code:

    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
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • You should read [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation) – ctwheels May 30 '18 at 15:33

1 Answers1

0

This is something you normally do when creating or changing a password. I normally use something like the following function to validate the complexity of a password:

'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

It is key that you always test your Regex, it is a little bit more complex than some think.

Now use this Function to validate your password and determine is continuing or not. Something like:

If Not validatePassword(txtNewPassword.Text) Then
  MsgBox("Password need to be...", vbCritical, "Password not in compliance...")
End If

You should allow all characters including Unicode, and should not restrict the length more than strictly necessary, for example if MySQL limit it. You should encourage a combination of letters, numbers, caps, etc.

Nandostyle

Nandostyle
  • 344
  • 2
  • 12