Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hey guys

How can I create a random Password Generator?

VB
Module Module1

    Sub Main()
        Dim passwordlength As String
        Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?!" 'All characters which will be used for the random passowrd        

         Dim r As Random
        Dim i As Integer
        Console.WriteLine("-- Passwort-Generator --")
        Console.WriteLine("Choose the Password length")
        passwordlength = Console.ReadLine() 'The length of the Password which will be generated
        For i = 1 To passwordlength
        Next

        Randomize()



How can I randomly choose the characters out of my "character pool" (The string "s")

For example (that everyone understand what I want to program): The users answers with 9 on the Password length question. Now the program should RANDOMLY pick 9 characters out of all the characters which are defined in the string "s".

Sorry for my English, Im coming from Switzerland :)
Posted
Updated 25-Sep-18 7:30am
v2
Comments
PIEBALDconsult 17-Dec-14 10:22am    
Why? A fully random password is very difficult remember and will likely lead to it being written down.
What I have found is that the use of base-64 can lead to a random-looking password that meets the criteria of the systems I use.
For example, "password" becomes "cGFzc3dvcmQ=", then I only need to remember the simple word I used.
YouCanHateMeNow 17-Dec-14 10:25am    
I know, ist just a quest from our IT teacher ;)

It doesn't have to be the super-safe tool which will get used in real surroundings.
PIEBALDconsult 17-Dec-14 10:29am    
Ah, so it's homework. We don't do that here.
Sergey Alexandrovich Kryukov 17-Dec-14 11:04am    
Not safe password doesn't need to be generated...
The problem is: despite of the fact you presented some code, the post could be classified as "no effort" (I did not report it though). Formally, you put some code, but you didn't even try to generate any password. Why? It doesn't seem to be difficult...
—SA
Sergey Alexandrovich Kryukov 17-Dec-14 11:01am    
Still, random password generators are widely used. (I don't know how many users use the passwords generated though.) The users can use the tools consolidating the set of passwords under a "master password", such as open-source KeePass.exe.

I think you are right; hardly many use the generated passwords; but it cannot make an excuse for not offering password generators...

—SA

You already declared a variable of Random class so you are on a good way. You basically randomly pick 9 characters from the string of allowed characters and store them in an array. Wne you are done you just create the password string from that array:
VB
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?!" 
Dim r As New Random
Const passwordLength As Integer = 9
Dim passwordChars() As Char = New Char(passwordLength - 1) {}
Dim charIndex As Integer

For i As Integer = 0 To passwordLength - 1
  charIndex = r.Next(s.Length)
  passwordChars(i) = s(charIndex)
Next

Dim password As New String(passwordChars)
 
Share this answer
 
You need to make a use of the Random class, to generate Random numbers. But, that would give you a Random integer, you can then use those numbers to call different alphabets at those indices.

You can read my previous article to create Random strings for URLs, instead of using them in URLs you can use it as a password. It generated random strings, and you can use it in your application too.

Understanding the Random URLs and Creating them in ASP.NET[^]

Use this converter[^] to convert the C# code to VB.NET.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900