-
Feb 15th, 2012, 02:50 PM
#1
Thread Starter
Fanatic Member
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:
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 = "0123456789"
Private Const Hexdecimal = "abcdef"
Private Const SpecialChars = "!@#$%^&*()<>=/\+-*"
Public Enum TGenPass
Alpha = 0
AlphaDigit = 1
Digits = 2
Hexdecimal = 3
SpecialChar = 4
SpecialAlpha = 5
SpecialDigit = 6
End Enum
Private Function GetPassword(ByVal Length As Integer, Optional ByVal GenType As TGenPass = TGenPass.Alpha) As String
Dim x As Integer
Dim r As Integer
Dim CharStream As String
Dim sb As StringBuilder
sb = New StringBuilder
CharStream = vbNullString
'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
End Select
For x = 1 To Length
'Ini Random numbers
Call Randomize()
'Get random number
r = Int(Rnd() * Len(CharStream) + 1)
'Build generated password.
Call sb.Append(Mid$(CharStream, r, 1))
Next x
GetPassword = sb.ToString
End Function
Public Property PasswordType As TGenPass
Get
'Return password type.
PasswordType = 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.
PasswordLength = 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.
Password = GetPassword(PasswordLength, PasswordType)
End Get
End Property
End Class
For the example add this to a command button.
vbnet Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim GenPws As New cPwsGen
'Set password generator props
GenPws.PasswordLength = 8
GenPws.PasswordType = cPwsGen.TGenPass.AlphaDigit
'Example
Call MsgBox(GenPws.Password, vbInformation Or vbOKOnly, "Your Passsword")
End Sub
-
Feb 15th, 2012, 06:20 PM
#2
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 = "!@#$%^&*()<>=/\+-*"
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
-
Feb 16th, 2012, 03:12 AM
#3
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|