Monday, March 31, 2014

vb.net generate Random password function

 Public Shared Function GenerateRandomPassword(size As Integer) As String
        'Const Alphabet As String = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        Const Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Const numberical As String = "1234567890565230"
        Dim rand As New Random()
        Dim chars As Char() = New Char(size - 1) {}
        For i As Integer = 0 To size - 1
            If (i <= 4) Then
                chars(i) = numberical(rand.Next(numberical.Length))
            Else
                chars(i) = Alphabet(rand.Next(Alphabet.Length))
            End If
        Next
        Return New String(chars)
    End Function
function call
GenerateRandomPassword(8)
will generate password with 8 characters( the first 5 characters are numeric others will be capital letters)




No comments:

Post a Comment