CraigWardman.com

[ Simple Password Generator ]

/Blog/Post

Simple Password Generator

To generate random passwords I have written a simple class in VB.NET, which accepts a string containing valid password characters and then generates passwords of n length using the specified characters.

Public Class PasswordGenerator
        Private _pwChars As String
 
        Public Sub New()
            MyClass.New("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
        End Sub
 
        Public Sub New(ByVal validChars As String)
            _pwChars = validChars
        End Sub
 
        Public Function GeneratePassword(ByVal length As Integer) As String
            Dim password As String = ""
            Dim rndNum As New Random()
 
            For i As Integer = 1 To length
                password &= _pwChars.Substring(rndNum.Next(0, _pwChars.Length - 1), 1)
            Next
 
            Return password
        End Function
 
    End Class

Share This!

Leave a Reply

*
CraigWardman.com
Car Leasing | Lease Cars