1

我正在尝试创建一个安全的随机密码生成器作为一个自学 VB.Net 的小项目,我遇到了以下C#文章

在尝试将其转换为 VB.Net 后,我​​的程序只是根据我的组合框选择吐出一个长度不同的“0”字符串。

我想了解我做错了什么,因为这对我来说是一次学习经历,所以任何帮助将不胜感激。

Imports System.Security.Cryptography
Imports System.Text

Public Class Form1
    Dim randomBytes() As Byte
    Dim randomInt32Value As Integer
    Dim possibleChars As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Security.Cryptography.RandomNumberGenerator.Create.GetBytes(randomBytes)
        randomInt32Value = BitConverter.ToInt32(randomBytes, 0)
    End Sub

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim builder As New StringBuilder

        For value1 As Integer = 0 To ComboBox1.SelectedIndex
            Dim r = New Random(randomInt32Value)
            possibleChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()"
            Dim nextInt As Integer = r.Next(possibleChars.Length)
            Dim c As Char = possibleChars(nextInt)
            builder.Append(c)
        Next

        Label1.Text = builder.ToString()
    End Sub
End Class
4

1 回答 1

0

看起来你缺少一些东西。我创建了 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
于 2013-09-23T16:48:18.250 回答