Trying to make a random password generator in VB.NET
So far have got this, but it only returns the first section of what im trying to do
I have 3 inputs for Type of password as checkboxes, in this order:
Numeric
Alphabetic
Symbols
If i have numeric checked it returns a numeric password, but if i have numeric and alphabetic checked it only returns a numeric password, although if i uncheck numeric and only have alphabetic checked it then returns and alphabetic password
An alphabetic password also has three options:
Uppercase
Lowercase
Mixed Case
Which when used with alphabetic actually returns the correct password
Here is the code i have so far:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
C_Numeric.Checked = True
R_Upper.Checked = True
End Sub
Private Sub B_Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_Generate.Click
Dim c_a As Boolean = False
Dim c_b As Boolean = False
Dim c_c As Boolean = False
Dim a As Integer
If C_Numeric.Checked = True Then
c_a = True
ElseIf C_Alphabetic.Checked = True Then
c_b = True
ElseIf C_Symbols.Checked = True Then
c_c = True
End If
If R_Lower.Checked = True Then
a = 1
ElseIf R_Upper.Checked = True Then
a = 2
ElseIf R_Mixed.Checked = True Then
a = 3
End If
If C_Numeric.Checked = True Or C_Alphabetic.Checked = True Or C_Symbols.Checked = True Then
TextBox1.Text = GenPass(NumericUpDown1.Value, c_a, c_b, c_c, a)
End If
End Sub
Function GenPass(ByVal Length As Integer, ByVal Num As Boolean, ByVal Alp As Boolean, ByVal Ascii As Boolean, ByVal Complexity As Integer)
Dim rand As New Random
Dim Pass As String = ""
Do Until Pass.Length = Length
Dim a As Integer
a = rand.Next(1, 3 + 1)
If a = 1 And Num = True Then
Pass += ChrW(rand.Next(Asc("0"), Asc("9") + 1))
End If
If a = 2 And Alp = True Then
If Complexity = 1 Then
Pass += ChrW(rand.Next(Asc("a"), Asc("z") + 1))
ElseIf Complexity = 2 Then
Pass += ChrW(rand.Next(Asc("A"), Asc("Z") + 1))
ElseIf Complexity = 3 Then
Dim b As Integer
b = rand.Next(1, 2 + 1)
If b = 1 Then
Pass += ChrW(rand.Next(Asc("A"), Asc("Z") + 1))
ElseIf b = 2 Then
Pass += ChrW(rand.Next(Asc("a"), Asc("z") + 1))
End If
End If
End If
If a = 3 And Ascii = True Then
Dim b As Integer
b = rand.Next(1, 4 + 1)
If b = 1 Then
Pass += ChrW(rand.Next(Asc("!"), Asc("/") + 1))
ElseIf b = 2 Then
Pass += ChrW(rand.Next(Asc(":"), Asc("@") + 1))
ElseIf b = 3 Then
Pass += ChrW(rand.Next(Asc("["), Asc("`") + 1))
ElseIf b = 4 Then
Pass += ChrW(rand.Next(Asc("{"), Asc("~") + 1))
End If
End If
Loop
Return (Pass)
End Function
For Example (Assume Length is 16 for all answers):
Having Numeric and Alphabetic selected with Mixed Case Should return:
eVOv3fyTmW7mvH24 CZOXVzeo1EzLu7Al V313p9VLW0Bz7Zfi
But instead returns:
8343299372194893 7303963979299152 3918539496952829
I Have not got a clue why is it not actually returning the desired results
Any help would be appreciated
Adam
It’s because of
So if
C_NumericandC_Alphabeticare checked,c_agetsTrue, but the line settingc_btoTruewon’t be hit due to theElseIf.So, remove the
Elsepart.Also, you could simplify this code by writting:
instead of the
Ifclauses, or better, getting rid of those unnecessary variables by just calling:There are a lot more improvements possbile. For example, instead of passing the
Complexityparameter asInteger, create anEnum:If you’re using VB 10.0 (introduced with .Net 4.0), you can get rid of the clunky
ByValkeyword. Also, the .Net Naming Guidelines in.Some more notes:
If you’re checking a boolean value in an
Ifclause, there’s no need to explicitly write= True:could be written as
Note that
AndAlsois generally better suited asAnddue to it’s short-circut behaviour.Also, better name your parameter
useAciiinstead ofAcii. It will make it more clearer.