문제

I'm trying to create a secure random password generator as a little project to teach myself VB.Net, and I came across the following article for C#.

After trying to convert it into VB.Net, my program just spits out a string of "0" of varying length depending on my combobox selection.

I'd like to understand what I've done wrong, as it's a learning experience for me, so any help would be greatly appreciated.

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
도움이 되었습니까?

해결책

It looks like you are missing a few things. I created 2 classes and wrote this up. It give me a random password every time. Enjoy.

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top