Avatar billede danielv Nybegynder
08. oktober 2007 - 13:30

Automatisk oprettelse af random bruger

Hej allesammen


Jeg sidder her og vil gerne lave et script som kan køres hver gang der skal oprettes en ny, tilfældig, bruger med, tilfældig, adgangskode.

Tanken er, at jeg vil bruge følgende kode til at skabe det tilfældige brugernavn:

Imports System
Imports System.Security.Cryptography

Module Module1


    Public Class RandomPassword


        Private Shared DEFAULT_MIN_PASSWORD_LENGTH As Integer = 4
        Private Shared DEFAULT_MAX_PASSWORD_LENGTH As Integer = 12


        Private Shared PASSWORD_CHARS_LCASE As String = "abcdefgijkmnopqrstwxyz"
        Private Shared PASSWORD_CHARS_UCASE As String = "ABCDEFGHJKLMNPQRSTWXYZ"
        Private Shared PASSWORD_CHARS_NUMERIC As String = "23456789"


        Public Shared Function Generate() As String
            Generate = Generate(DEFAULT_MIN_PASSWORD_LENGTH, _
                                DEFAULT_MAX_PASSWORD_LENGTH)
        End Function


        Public Shared Function Generate(ByVal length As Integer) As String
            Generate = Generate(length, length)
        End Function


        Public Shared Function Generate(ByVal minLength As Integer, _
        ByVal maxLength As Integer) _
            As String


            If (minLength <= 0 Or maxLength <= 0 Or minLength > maxLength) Then
                Generate = Nothing
            End If


            Dim charGroups As Char()() = New Char()() _
            { _
                PASSWORD_CHARS_LCASE.ToCharArray(), _
                PASSWORD_CHARS_UCASE.ToCharArray(), _
                PASSWORD_CHARS_NUMERIC.ToCharArray(), _
                PASSWORD_CHARS_SPECIAL.ToCharArray() _
            }

            Dim charsLeftInGroup As Integer() = New Integer(charGroups.Length - 1) {}

            Dim I As Integer
            For I = 0 To charsLeftInGroup.Length - 1
                charsLeftInGroup(I) = charGroups(I).Length
            Next

            Dim leftGroupsOrder As Integer() = New Integer(charGroups.Length - 1) {}

            For I = 0 To leftGroupsOrder.Length - 1
                leftGroupsOrder(I) = I
            Next


            Dim randomBytes As Byte() = New Byte(3) {}

            Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider()

            rng.GetBytes(randomBytes)

            Dim seed As Integer = ((randomBytes(0) And &H7F) << 24 Or _
                                    randomBytes(1) << 16 Or _
                                    randomBytes(2) << 8 Or _
                                    randomBytes(3))

            Dim random As Random = New Random(seed)

            Dim password As Char() = Nothing

            If (minLength < maxLength) Then
                password = New Char(random.Next(minLength - 1, maxLength)) {}
            Else
                password = New Char(minLength - 1) {}
            End If

            Dim nextCharIdx As Integer

            Dim nextGroupIdx As Integer

            Dim nextLeftGroupsOrderIdx As Integer

            Dim lastCharIdx As Integer

            Dim lastLeftGroupsOrderIdx As Integer = leftGroupsOrder.Length - 1

            For I = 0 To password.Length - 1


                If (lastLeftGroupsOrderIdx = 0) Then
                    nextLeftGroupsOrderIdx = 0
                Else
                    nextLeftGroupsOrderIdx = random.Next(0, lastLeftGroupsOrderIdx)
                End If


                nextGroupIdx = leftGroupsOrder(nextLeftGroupsOrderIdx)

                lastCharIdx = charsLeftInGroup(nextGroupIdx) - 1


                If (lastCharIdx = 0) Then
                    nextCharIdx = 0
                Else
                    nextCharIdx = random.Next(0, lastCharIdx + 1)
                End If

                password(I) = charGroups(nextGroupIdx)(nextCharIdx)

                If (lastCharIdx = 0) Then
                    charsLeftInGroup(nextGroupIdx) = _
                                    charGroups(nextGroupIdx).Length

                Else


                    If (lastCharIdx <> nextCharIdx) Then
                        Dim temp As Char = charGroups(nextGroupIdx)(lastCharIdx)
                        charGroups(nextGroupIdx)(lastCharIdx) = _
                                    charGroups(nextGroupIdx)(nextCharIdx)
                        charGroups(nextGroupIdx)(nextCharIdx) = temp
                    End If


                    charsLeftInGroup(nextGroupIdx) = _
                              charsLeftInGroup(nextGroupIdx) - 1
                End If

                If (lastLeftGroupsOrderIdx = 0) Then
                    lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1

                Else



                    If (lastLeftGroupsOrderIdx <> nextLeftGroupsOrderIdx) Then
                        Dim temp As Integer = _
                                    leftGroupsOrder(lastLeftGroupsOrderIdx)
                        leftGroupsOrder(lastLeftGroupsOrderIdx) = _
                                    leftGroupsOrder(nextLeftGroupsOrderIdx)
                        leftGroupsOrder(nextLeftGroupsOrderIdx) = temp
                    End If


                    lastLeftGroupsOrderIdx = lastLeftGroupsOrderIdx - 1
                End If
            Next


            Generate = New String(password)
        End Function

    End Class


    Sub Main()
        Dim I As Integer
        For I = 1 To 100
            Console.WriteLine(RandomPassword.Generate(4, 12))
        Next
    End Sub

End Module


Og følgende kode til at skabe en tilfældig adganskode:

Imports System
Imports System.Security.Cryptography

Module Module1


    Public Class RandomPassword


        Private Shared DEFAULT_MIN_PASSWORD_LENGTH As Integer = 4
        Private Shared DEFAULT_MAX_PASSWORD_LENGTH As Integer = 12


        Private Shared PASSWORD_CHARS_LCASE As String = "abcdefgijkmnopqrstwxyz"
        Private Shared PASSWORD_CHARS_UCASE As String = "ABCDEFGHJKLMNPQRSTWXYZ"
        Private Shared PASSWORD_CHARS_NUMERIC As String = "23456789"
        Private Shared PASSWORD_CHARS_SPECIAL As String = "!#¤%&/()=?@£${[]}"


        Public Shared Function Generate() As String
            Generate = Generate(DEFAULT_MIN_PASSWORD_LENGTH, _
                                DEFAULT_MAX_PASSWORD_LENGTH)
        End Function


        Public Shared Function Generate(ByVal length As Integer) As String
            Generate = Generate(length, length)
        End Function


        Public Shared Function Generate(ByVal minLength As Integer, _
        ByVal maxLength As Integer) _
            As String


            If (minLength <= 0 Or maxLength <= 0 Or minLength > maxLength) Then
                Generate = Nothing
            End If


            Dim charGroups As Char()() = New Char()() _
            { _
                PASSWORD_CHARS_LCASE.ToCharArray(), _
                PASSWORD_CHARS_UCASE.ToCharArray(), _
                PASSWORD_CHARS_NUMERIC.ToCharArray(), _
                PASSWORD_CHARS_SPECIAL.ToCharArray() _
            }

            Dim charsLeftInGroup As Integer() = New Integer(charGroups.Length - 1) {}

            Dim I As Integer
            For I = 0 To charsLeftInGroup.Length - 1
                charsLeftInGroup(I) = charGroups(I).Length
            Next

            Dim leftGroupsOrder As Integer() = New Integer(charGroups.Length - 1) {}

            For I = 0 To leftGroupsOrder.Length - 1
                leftGroupsOrder(I) = I
            Next


            Dim randomBytes As Byte() = New Byte(3) {}

            Dim rng As RNGCryptoServiceProvider = New RNGCryptoServiceProvider()

            rng.GetBytes(randomBytes)

            Dim seed As Integer = ((randomBytes(0) And &H7F) << 24 Or _
                                    randomBytes(1) << 16 Or _
                                    randomBytes(2) << 8 Or _
                                    randomBytes(3))

            Dim random As Random = New Random(seed)

            Dim password As Char() = Nothing

            If (minLength < maxLength) Then
                password = New Char(random.Next(minLength - 1, maxLength)) {}
            Else
                password = New Char(minLength - 1) {}
            End If

            Dim nextCharIdx As Integer

            Dim nextGroupIdx As Integer

            Dim nextLeftGroupsOrderIdx As Integer

            Dim lastCharIdx As Integer

            Dim lastLeftGroupsOrderIdx As Integer = leftGroupsOrder.Length - 1

            For I = 0 To password.Length - 1


                If (lastLeftGroupsOrderIdx = 0) Then
                    nextLeftGroupsOrderIdx = 0
                Else
                    nextLeftGroupsOrderIdx = random.Next(0, lastLeftGroupsOrderIdx)
                End If


                nextGroupIdx = leftGroupsOrder(nextLeftGroupsOrderIdx)

                lastCharIdx = charsLeftInGroup(nextGroupIdx) - 1


                If (lastCharIdx = 0) Then
                    nextCharIdx = 0
                Else
                    nextCharIdx = random.Next(0, lastCharIdx + 1)
                End If

                password(I) = charGroups(nextGroupIdx)(nextCharIdx)

                If (lastCharIdx = 0) Then
                    charsLeftInGroup(nextGroupIdx) = _
                                    charGroups(nextGroupIdx).Length

                Else


                    If (lastCharIdx <> nextCharIdx) Then
                        Dim temp As Char = charGroups(nextGroupIdx)(lastCharIdx)
                        charGroups(nextGroupIdx)(lastCharIdx) = _
                                    charGroups(nextGroupIdx)(nextCharIdx)
                        charGroups(nextGroupIdx)(nextCharIdx) = temp
                    End If


                    charsLeftInGroup(nextGroupIdx) = _
                              charsLeftInGroup(nextGroupIdx) - 1
                End If

                If (lastLeftGroupsOrderIdx = 0) Then
                    lastLeftGroupsOrderIdx = leftGroupsOrder.Length - 1

                Else



                    If (lastLeftGroupsOrderIdx <> nextLeftGroupsOrderIdx) Then
                        Dim temp As Integer = _
                                    leftGroupsOrder(lastLeftGroupsOrderIdx)
                        leftGroupsOrder(lastLeftGroupsOrderIdx) = _
                                    leftGroupsOrder(nextLeftGroupsOrderIdx)
                        leftGroupsOrder(nextLeftGroupsOrderIdx) = temp
                    End If


                    lastLeftGroupsOrderIdx = lastLeftGroupsOrderIdx - 1
                End If
            Next


            Generate = New String(password)
        End Function

    End Class


    Sub Main()
        Dim I As Integer
        For I = 1 To 100
            Console.WriteLine(RandomPassword.Generate(4, 12))
        Next
    End Sub

End Module

(Sådanset samme kode, bare med lidt forskellige forudsætninger.)

Og som et sidste tvist skal der være mulighed for at sætte en vilkårlig tidsbegrænsning for eksistensen af brugeren.

Som det måske kan regnes ud, så skal det bruges til en form for hotspotløsning hvor man køber sig til X antal tid og dermed får sig en tilfældig bruger og adgangskode til systemet.

Håber virkelig at der er nogen der kan hjælpe mig med dette; en hver hjælp vil blive modtaget med største taknemmelighed!


Med venlig hilsen Daniel...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester