尝试修改代码,使其获取变量值并根据变量值生成密码

时间:2013-09-27 00:25:05

标签: vb.net visual-studio-2012

我正在尝试这样做,以便当我单击btnSave时,它从textbox1获取值并根据值创建X密码。我已将密码生成代码和我的代码包含在保存按钮中。任何帮助将不胜感激,因为我不知道如何管理这个。我尝试过使用数组但是我的朋友告诉我,数组不适合这个任务。有什么输入吗?

Public Class Form1
Private randomBytes() As Byte
Private randomInt32Value As Integer
Private possibleChars As String
Private len As Int32
Private GetRandomInt32Value As New RandomInt32Value

Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
    If cbSymbols.Checked Then
        possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&"
    Else
        possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    End If
    Try
        Dim cpossibleChars() As Char
        cpossibleChars = possibleChars.ToCharArray()
        If cpossibleChars.Length < 1 Then
            MessageBox.Show("You must enter one or more possible characters.")
            Return
        End If
        If len < 4 Then
            MessageBox.Show(String.Format("Please choose a password length. That length must be a value between {0} and {1}. Note: values above 1,000 might take a LONG TIME to process on some computers.", 4, Int32.MaxValue))
            Return
        End If

        Dim builder As New StringBuilder()

        For i As Integer = 0 To ComboBox1.SelectedIndex + 5
            Dim randInt32 As Integer = GetRandomInt32Value.GetRandomInt()
            Dim r As New Random(randInt32)

            Dim nextInt As Integer = r.[Next](cpossibleChars.Length)
            Dim c As Char = cpossibleChars(nextInt)
            builder.Append(c)
        Next
        Me.Label1.Text = builder.ToString()
    Catch ex As Exception
        MessageBox.Show(String.Format("An error has occurred while trying to generate random password! Technical description: {0}", ex.Message.ToString()))
    End Try
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim myStream As Stream
    Dim saveFileDialog1 As New SaveFileDialog()
    Dim saveFileName As String = ""
    Dim index As Integer = 0
    If TextBox1.Text = Nothing Then
        MsgBox("Enter a number of passwords to generate!")
    Else
        Dim genPasswords As New List(Of String)
        saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
        saveFileDialog1.RestoreDirectory = True
        saveFileDialog1.FileName = "secure.txt"

        If saveFileDialog1.ShowDialog() = DialogResult.OK Then
            myStream = saveFileDialog1.OpenFile()

            Using sw As StreamWriter = New StreamWriter(myStream)
                saveFileName = Path.GetFileName(saveFileDialog1.FileName)
                sw.WriteLine(Date.Today + " - " + saveFileName) ' Date and File title header
                sw.WriteLine("-------------------")

                sw.Close()
            End Using

            If (myStream IsNot Nothing) Then
                ' Code to write the stream goes here.
                myStream.Close()
            End If
        End If
    End If
End Sub

End Class


Public Class RandomInt32Value
Public Function GetRandomInt() As Integer
    Dim randomBytes As Byte() = New Byte(3) {}
    Dim rng As New RNGCryptoServiceProvider()
    rng.GetBytes(randomBytes)
    Dim randomInt As Integer = BitConverter.ToInt32(randomBytes, 0)
    Return randomInt
End Function
End Class

0 个答案:

没有答案