在 VB.Net 中创建一个安全的随机密码生成器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18963720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a secure random password generator in VB.Net
提问by
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 articlefor C#.
我正在尝试创建一个安全的随机密码生成器作为一个小项目来自学 VB.Net,并且我遇到了以下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.
在尝试将其转换为 VB.Net 后,我的程序只是根据我的组合框选择吐出一串不同长度的“0”。
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
采纳答案by Bobb Opie
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.
看起来您缺少一些东西。我创建了 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

