0

尝试在 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

我不知道为什么它实际上没有返回所需的结果

任何帮助,将不胜感激

亚当

4

2 回答 2

3

这是因为

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

因此,如果C_NumericC_Alphabetic被选中,则得到c_aTruec_b由于.TrueElseIf

所以,删除Else部分。


此外,您可以通过编写以下代码来简化此代码:

c_a = C_Numeric.Checked 
c_b = C_Alphabetic.Checked 
c_c = C_Symbols.Checked 

而不是If子句,或者更好的是,只需调用即可摆脱那些不必要的变量:

GenPass(NumericUpDown1.Value, C_Numeric.Checked, C_Alphabetic.Checked, C_Symbols.Checked, a)

还有很多改进可能。例如,不要将Complexity参数作为传递,而是Integer创建一个Enum

Enum Complexity
    LowerOnly
    UpperOnly
    Mixed
End Enum

Function GenPass(Length As Integer, Num As Boolean, Alp As Boolean, Ascii As Boolean, Complexity As Complexity)

如果您使用的是 VB 10.0(由 .Net 4.0 引入),您可以摆脱笨重的ByVal关键字。此外,.Net 命名指南在。


还有一些注意事项:

如果您在If子句中检查布尔值,则无需显式编写= True

If a = 3 And Ascii = True Then

可以写成

If a = 3 AndAlso Ascii Then

请注意,由于它的短路行为,AndAlso通常更适合。And

此外,最好命名您的参数useAcii而不是Acii. 它会让它更清晰。

于 2012-08-27T08:26:42.653 回答
0

您可以使用以下代码来获得所需的结果。

If C_Numeric.Checked = True Then
    c_a = True
End If
If C_Alphabetic.Checked = True Then
    c_b = True
End If
If C_Symbols.Checked = True Then
    c_c = True
End If
于 2012-08-27T08:36:24.273 回答