Results 1 to 3 of 3

Thread: Password Generator class

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Password Generator class

    Hi this is just a small class I made for one of my projects to generate a random password from letters,numbers and special chars easy to add to your project. Hope you find it usfull

    Create a new project add a class call it cPwsGen.vb
    Add the code below to the class.

    vbnet Code:
    1. Imports System.Text
    2.  
    3. Public Class cPwsGen
    4.     'Private variables.
    5.     Private pLen As Integer = 0
    6.     Private pPasswordType As TGenPass
    7.  
    8.     'Private password sources
    9.     Private Const Alpha As String = "abcdefghijklmnopqrstuvwxyz"
    10.     Private Const Digits = "0123456789"
    11.     Private Const Hexdecimal = "abcdef"
    12.     Private Const SpecialChars = "!@#$%^&*()<>=/\+-*"
    13.  
    14.     Public Enum TGenPass
    15.         Alpha = 0
    16.         AlphaDigit = 1
    17.         Digits = 2
    18.         Hexdecimal = 3
    19.         SpecialChar = 4
    20.         SpecialAlpha = 5
    21.         SpecialDigit = 6
    22.     End Enum
    23.  
    24.     Private Function GetPassword(ByVal Length As Integer, Optional ByVal GenType As TGenPass = TGenPass.Alpha) As String
    25.         Dim x As Integer
    26.         Dim r As Integer
    27.         Dim CharStream As String
    28.         Dim sb As StringBuilder
    29.  
    30.         sb = New StringBuilder
    31.         CharStream = vbNullString
    32.  
    33.         'Password types
    34.         Select Case GenType
    35.             Case TGenPass.Alpha
    36.                 CharStream = Alpha
    37.             Case TGenPass.Digits
    38.                 CharStream = Digits
    39.             Case TGenPass.Hexdecimal
    40.                 CharStream = Hexdecimal & Digits
    41.             Case TGenPass.AlphaDigit
    42.                 CharStream = Digits & Alpha
    43.             Case TGenPass.SpecialChar
    44.                 CharStream = SpecialChars
    45.             Case TGenPass.SpecialAlpha
    46.                 CharStream = Alpha & SpecialChars
    47.             Case TGenPass.SpecialDigit
    48.                 CharStream = Digits & SpecialChars
    49.         End Select
    50.  
    51.         For x = 1 To Length
    52.             'Ini Random numbers
    53.             Call Randomize()
    54.             'Get random number
    55.             r = Int(Rnd() * Len(CharStream) + 1)
    56.             'Build generated password.
    57.             Call sb.Append(Mid$(CharStream, r, 1))
    58.         Next x
    59.  
    60.         GetPassword = sb.ToString
    61.     End Function
    62.  
    63.     Public Property PasswordType As TGenPass
    64.         Get
    65.             'Return password type.
    66.             PasswordType = pPasswordType
    67.         End Get
    68.         Set(ByVal value As TGenPass)
    69.             'Set password type.
    70.             pPasswordType = value
    71.         End Set
    72.     End Property
    73.  
    74.     Public Property PasswordLength As Integer
    75.         Get
    76.             'Return password length.
    77.             PasswordLength = pLen
    78.         End Get
    79.         Set(ByVal value As Integer)
    80.             'Set password length.
    81.             pLen = value
    82.         End Set
    83.     End Property
    84.  
    85.     Public ReadOnly Property Password As String
    86.         Get
    87.             'Return generated password.
    88.             Password = GetPassword(PasswordLength, PasswordType)
    89.         End Get
    90.     End Property
    91. End Class

    For the example add this to a command button.
    vbnet Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim GenPws As New cPwsGen
    3.         'Set password generator props
    4.         GenPws.PasswordLength = 8
    5.         GenPws.PasswordType = cPwsGen.TGenPass.AlphaDigit
    6.         'Example
    7.         Call MsgBox(GenPws.Password, vbInformation Or vbOKOnly, "Your Passsword")
    8.     End Sub

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Password Generator class

    Your code is stuck in VB6! Here's a translation to VB.NET:

    Code:
    Imports System.Text
    
    Public Class cPwsGen
        'Private variables.
        Private pLen As Integer = 0
        Private pPasswordType As TGenPass
    
        'Private password sources
        Private Const Alpha As String = "abcdefghijklmnopqrstuvwxyz"
        Private Const Digits As String = "0123456789"
        Private Const Hexdecimal As String = "abcdef"
        Private Const SpecialChars As String = "!@#$&#37;^&*()<>=/\+-*"
    
        Public Enum TGenPass
            Alpha
            AlphaDigit
            Digits
            Hexdecimal
            SpecialChar
            SpecialAlpha
            SpecialDigit
        End Enum
    
        Private Function GetPassword(ByVal Length As Integer, Optional ByVal GenType As TGenPass = TGenPass.Alpha) As String
            Dim r As New Random()
            Dim sb As New StringBuilder()
            Dim CharStream As String
    
            'Password types
            Select Case GenType
                Case TGenPass.Alpha
                    CharStream = Alpha
                Case TGenPass.Digits
                    CharStream = Digits
                Case TGenPass.Hexdecimal
                    CharStream = Hexdecimal & Digits
                Case TGenPass.AlphaDigit
                    CharStream = Digits & Alpha
                Case TGenPass.SpecialChar
                    CharStream = SpecialChars
                Case TGenPass.SpecialAlpha
                    CharStream = Alpha & SpecialChars
                Case TGenPass.SpecialDigit
                    CharStream = Digits & SpecialChars
                Case Else
                    Throw New ArgumentException("Invalid generation type!")
            End Select
    
            For x As Integer = 1 To Length
                'Add a random character to the password:
                sb.Append(CharStream(r.Next(0, CharStream.Length)))
            Next
    
            Return sb.ToString()
        End Function
    
        Public Property PasswordType As TGenPass
            Get
                'Return password type.
                Return pPasswordType
            End Get
            Set(ByVal value As TGenPass)
                'Set password type.
                pPasswordType = value
            End Set
        End Property
    
        Public Property PasswordLength As Integer
            Get
                'Return password length.
                Return pLen
            End Get
            Set(ByVal value As Integer)
                'Set password length.
                pLen = value
            End Set
        End Property
    
        Public ReadOnly Property Password As String
            Get
                'Return generated password.
                Return GetPassword(PasswordLength, PasswordType)
            End Get
        End Property
    End Class

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: Password Generator class

    Thanks for the convert I am still new at VB.NET So any fixes help thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width