看起来你缺少一些东西。我创建了 2 个类并写了这个。它每次都会给我一个随机密码。享受。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Security.Cryptography
Imports System.Windows.Forms
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 GetPasswordGenProfiler As New PasswordGenProfiler
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"
len = 8
End Sub
Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
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 len - 1
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
End Class
Public Class PasswordGenProfiler
Public Shared Function GetFrequencyDistributionOfChars(allowableChars As String, generatedPass As String) As Dictionary(Of Char, Integer)
Dim distrib As New Dictionary(Of Char, Integer)()
' initialize all values to 0
For Each c As Char In allowableChars
' If character is listed more than once, don't re-add it to our list.
If Not distrib.ContainsKey(c) Then
distrib.Add(c, 0)
End If
Next
Dim val As Integer = 0
For Each passChar As Char In generatedPass
If distrib.TryGetValue(passChar, val) Then
distrib(passChar) = System.Threading.Interlocked.Increment(val)
End If
Next
Return distrib
End Function
End Class
Imports System.Security.Cryptography
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