我正在尝试这样做,以便当我单击 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