[JAVA CODE] Password Generator - Commented and noobish friendly 01-09-2013, 06:53 PM
#1
Hello [username], here is a program i designed =D
It's a password generator.
NOTE: I'm Already working on a version two of this project. Thank you Deque for showing the flaws \M/
It has the following features:
- 4 different Charsets:
----- Numbers (0-9)
----- Lower-Case Alpha (a-z)
----- Upper-Case Alpha (A-Z)
----- Special-Case (-_!#$%&.)
- Possibility to generate up 15 combinations of those Charsets:
----- Numbers Only
----- Lower-Case Only
----- Upper-Case Only
----- ...
----- ...
----- ...
----- - All The Charsets Together
- It works in two ways:
----- Random Password Generator Mode (produces a password with a length from 8 to 12 chars and chooses randomly one of the 15 possible charset combinations.
----- Defined Parameters Password Generator Mode (You define the length and charset combinations)
- The program is also protected against "troll users" - If it asks for an Integer Number You cannot insert something different.
This program is fully working and you can use the code and modify it but giving the proper credits
This was done with the intent of helping less experienced people and provide them with a working semi-semi-semi-semi complex program for them to tweak and understand it's way of working... Besides, who wouldn't like to play with a password generating program? XD
PS: THIS PROGRAM IS NOT OPTIMAL, THERE ARE CERTAIN PLACES WHERE THINGS COULD BE DONE MORE EFFECTIVELY AND CORRECTLY. BUT PLEASE DO POST YOUR CREATIVE COMMENTS, QUESTIONS, SUGGESTIONS, ETC ^^
DOWNLOADS:
> DOWNLOAD PROJECT (.java)
> DOWNLOAD RUNABLE JAR + BAT TO RUN IN WINDOWS (.jar + .bat)
NOTES:
- If "it doesn't work" copy the code, open eclipse, create a new java project, call it passGenerator, paste the code.
This "public class passGenerator" must be equal to the class name (which will also be the file name + .java)
Code:
import java.util.Random;
import java.util.Scanner;
public class passGenerator {
/**
* Program that generates passwords randomly or with a defined size and
* charset(s)
*
* @author N-genhocas
* @date 09/01/2013
*/
public static void main(String[] args) {
// Initializes Scanner (to read inputs from keyboard)
Scanner scan = new Scanner(System.in);
// Flag that when changed to true, terminates the program
boolean terminateProgramFlag = false;
// Will be used to determine if the input provided was correct
boolean correctInput = false;
// Will contain the option number (started with an invalid input in case
// something goes wrong)
int option = 4;
// Cicle keeps the program running until the user decides to terminate
// it
do {
// Prevents going to next step from main menu wiht invalid choice
do {
// Presents the Main Menu
System.out.println("---------------------------------");
System.out.println(" Password generating program ");
System.out.println("---------------------------------\n");
System.out.println("Select an option:");
System.out
.println("\n\t0 - Generate a random password (Random Parameters)");
System.out
.println("\t1 - Generate a random password (Selecting it's parameters)");
System.out.println("\t2 - Exit Program\n");
System.out.print("Option: ");
// Read the user input
String optionCheck = scan.nextLine();
// Checks if input is an integer
if (isInteger(optionCheck)) {
//Checks if in the required range
if (Integer.parseInt(optionCheck) >= 0
&& Integer.parseInt(optionCheck) < 3) {
option = Integer.parseInt(optionCheck);
correctInput = true;
} else {
displayBadInput();
}
} else
displayBadInput();
} while (correctInput == false);
switch (option) {
//Option 0 was selected - Generates a completely random password with 8 to 12 chars
case 0:
//Inicializes Random (to generate random numbers)
Random randNumber = new Random();
// Generate random password with random parameters
System.out.println("\n---------------------------------");
System.out.println(" Random Generated Password ");
System.out.println("---------------------------------\n");
System.out.print("Password: ");
//Generates a number between 8 and 12
generatePassword(randNumber.nextInt(4) + 8,
randNumber.nextInt(15) + 1);
System.out.println();
break;
//Option 1 was selected - Generates password with defined parameters
case 1:
//Initializing/atribution os values to variables
correctInput = false;
int value = 1;
int length = 0;
do {
System.out.println("\n---------------------------------");
System.out.println(" Configured Password Generation ");
System.out.println("---------------------------------\n");
System.out
.println("Choose the charset(s) by summing the values or just 0 to exit to main menu");
System.out.println("\n\t0 - EXIT (to Main Menu)");
System.out.println("\t1 - NUMBERS (0...9)");
System.out.println("\t2 - LOWER-CASE (a...z)");
System.out.println("\t4 - UPPER-CASE (A...Z)");
System.out.println("\t8 - SPECIAL CHARS (-_!#$%&.)");
System.out.print("Value: ");
String valueCheck = scan.nextLine();
// Check if the iput is an integer
//This block reads analyzes and determine if menu option selected was valid
if (isInteger(valueCheck) == true) {
//Check if value is within range
if (Integer.parseInt(valueCheck) >= 0
&& Integer.parseInt(valueCheck) < 16) {
value = Integer.valueOf(valueCheck);
if (value == 0)
break;
if (value > 0 && value < 16)
correctInput = true;
} else {
correctInput = false;
System.out
.println("> PLEASE CHOOSE A VALUE WITHIN THE RANGE (0-15)");
}
} else {
correctInput = false;
displayBadInput();
}
// Check if previous input was correct, no need to show/run this if it wasn't
//This block asks, reads and analyzes if length of password is valid
if (correctInput == true) {
System.out.print("\nChoose the length: ");
String lengthCheck = scan.nextLine();
if (isInteger(valueCheck) == true)
length = Integer.valueOf(lengthCheck);
else {
correctInput = false;
displayBadInput();
}
} else {
correctInput = false;
}
// Check if previous input was correct, no need to show/run this if it wasn't
if (correctInput == true) {
System.out.print("\nPassword: ");
generatePassword(length, value);
System.out.println();
}
} while (correctInput == false);
correctInput = false;
break;
//Option 2 was selected - The program will temrinate
case 2:
// Sets the flag to true so it skips the do cycle and terminates
// the program
terminateProgramFlag = true;
break;
}
} while (terminateProgramFlag != true);
}
/**
* Generates a String and prints it with Lenght (lenght) and Charset(charset
* from1 to 15)
*
* @param length
* - length of the password
* @param charset
* - Charset used in the password formation
*/
public static void generatePassword(int length, int charset) {
// Char arrany that contains all the numbers - 1
char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// Char array that contains all lower-case letters - 2
char[] lowerCase = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z' };
// Char array that contains all the upper-case letters - 4
char[] upperCase = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z' };
// Char array that contains special Symbols - 8
char[] special = { '-', '_', '!', '#', '$', '%', '&', '.' };
Random randNumber = new Random();
StringBuilder password;
switch (charset) {
// JUST NUMBER
case 1:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
System.out.println(password.toString());
break;
// JUST LOW
case 2:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
}
System.out.println(password.toString());
break;
// NUMBER, LOW
case 3:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(9);
password.append(number[position]);
} else {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
}
}
System.out.println(password.toString());
break;
// JUST UP
case 4:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
}
System.out.println(password.toString());
break;
// NUMBER, UP
case 5:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(9);
password.append(number[position]);
} else {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
}
}
System.out.println(password.toString());
break;
// UP, LOW
case 6:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
} else {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
}
}
System.out.println(password.toString());
break;
// UP, LOW, NUMBER
case 7:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(3);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
} else if (charsetType == 1) {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
} else {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
}
System.out.println(password.toString());
break;
// JUST SPECIAL
case 8:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int position = randNumber.nextInt(8);
password.append(special[position]);
}
System.out.println(password.toString());
break;
// SPECIAL, NUMBER
case 9:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(9);
password.append(number[position]);
} else {
int position = randNumber.nextInt(8);
password.append(special[position]);
}
}
System.out.println(password.toString());
break;
// SPECIAL, LOW
case 10:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
}
}
System.out.println(password.toString());
break;
// SPECIAL, LOW, NUMBER
case 11:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(3);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
} else if (charsetType == 1) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
}
System.out.println(password.toString());
break;
// SPECIAL, UP
case 12:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(2);
if (charsetType == 0) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
}
}
System.out.println(password.toString());
break;
// SPECIAL, UP, NUMBER
case 13:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(3);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
} else if (charsetType == 1) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
}
System.out.println(password.toString());
break;
// SPECIAL, UP, LOW
case 14:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(3);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
} else if (charsetType == 1) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
}
}
System.out.println(password.toString());
break;
// ALL
case 15:
password = new StringBuilder();
for (int i = 0; i < length; i++) {
int charsetType = randNumber.nextInt(4);
if (charsetType == 0) {
int position = randNumber.nextInt(26);
password.append(upperCase[position]);
} else if (charsetType == 1) {
int position = randNumber.nextInt(8);
password.append(special[position]);
} else if (charsetType == 2) {
int position = randNumber.nextInt(26);
password.append(lowerCase[position]);
} else {
int position = randNumber.nextInt(9);
password.append(number[position]);
}
}
System.out.println(password.toString());
break;
default:
System.out
.println("Incorrect Input! Please choose a number from 1 to 15!");
}
}
/**
* Displays a bad input message
*
*/
public static void displayBadInput() {
System.out
.println("\n\\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/");
System.out.println("Please choose a valid and Integer option!\n");
}
/**
* Checks if a string is an integer or not
*
* @param input
* - String to be analized
* @return true if string is composed by ints
* @return false - if string is not composed by ints
*/
public static boolean isInteger(String input) {
try {
Integer.parseInt(input);
return true;
} catch (Exception e) {
return false;
}
}
}