尝试在 VB.NET 中制作随机密码生成器
到目前为止已经得到了这个,但它只返回我想要做的第一部分
我有 3 个密码类型作为复选框的输入,按以下顺序:
Numeric
Alphabetic
Symbols
如果我检查了数字,它返回一个数字密码,但如果我检查了数字和字母,它只返回一个数字密码,尽管如果我取消选中数字并且只检查了字母,那么它返回和字母密码
字母密码也有三个选项:
Uppercase
Lowercase
Mixed Case
当与字母一起使用时,它实际上返回了正确的密码
这是我到目前为止的代码:
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
例如(假设所有答案的长度为 16):
使用混合大小写选择数字和字母应该返回:
eVOv3fyTmW7mvH24 CZOXVzeo1EzLu7Al V313p9VLW0Bz7Zfi
而是返回:
8343299372194893 7303963979299152 3918539496952829
我不知道为什么它实际上没有返回所需的结果
任何帮助,将不胜感激
亚当