secret
No... actually that is not such a great password. A password cracker would guess it in a few milliseconds.
Here is a program for generating random passwords:
import java.util.*;
public class PasswordGenerator
{
public static void main ( String[] args )
{
Scanner scan = new Scanner( System.in );
Random rand = new Random();
int digits = 0;
// Ensure that the password is 5 or more characters
while ( digits < 5 )
{
System.out.println("Your password must have at least 5 characters.");
System.out.print("How many characters do you want in your password? ");
digits = scan.nextInt();
}
// Create a long string of characters to randomly pick from
String choices = "abcdefghijklmnopqrstuvwxyz" ;
choices = choices + choices.toUpperCase() ;
choices = choices + "1234567890" ;
String password = "";
int j = 0;
// Randomly pick characters from the string
while ( j<digits )
{
password = password + choices.charAt( rand.nextInt( choices.length() ) );
j = j + 1;
}
System.out.println("Here is your password: " + password );
}
}
Here are a few runs of the program:
K:\>java PasswordGenerator Your password must have at least 5 characters. How many characters do you want in your password? 4 Your password must have at least 5 characters. How many characters do you want in your password? 3 Your password must have at least 5 characters. How many characters do you want in your password? 8 Here is your password: BaXpmUsA K:\>java PasswordGenerator Your password must have at leas 5 characters. How many characters do you want in your password? 12 Here is your password: ly3YFAhM8HDH
The details of this program are explained in the next few pages.
What is the purpose of the following loop from the program:
while ( digits < 5 )
{
System.out.println("Your password must have at least 5 characters.");
System.out.print("How many characters do you want in your password? ");
digits = scan.nextInt();
}