-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
104 lines (63 loc) · 2.39 KB
/
Main.java
File metadata and controls
104 lines (63 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Main{
public static char[] validChars;
public static void main(String[] args){
File file = new File("validCharacters.txt");
try {
Scanner scanner = new Scanner(file);
validChars = scanner.nextLine().toCharArray(); //Generates an array of valid charcters that the password may use.
scanner.close();
Scanner userIn = new Scanner(System.in);
System.out.print("How many characters in your password? ");
int charNum = userIn.nextInt();
System.out.print("How many passwords? ");
int passwordNum = userIn.nextInt();
userIn.close();
String[] passwordList = generator(charNum, passwordNum, new Random());
FileWriter fileWriter = new FileWriter("passwords.txt");
for(int i=0; i<passwordNum;i++){
fileWriter.write(passwordList[i]+"\n");
}
fileWriter.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
System.exit(0);
} catch (IOException e){
e.printStackTrace();
}
}
public static String[] generator(int charNum, int passwordNum, Random rand){
//Generates a list of valid passwords.
String[] passwords = new String[passwordNum];
for(int i=0; i<passwordNum; i++){
do{
passwords[i] = "";
for(int j=0; j<charNum; j++){
passwords[i] += (char) (rand.nextInt(94)+33);
}
} while(!checker(passwords[i]));
}
return passwords;
}
public static boolean checker(String password){
//Checks each generated password against the valid characters.
for(int i=0; i<password.length(); i++){
boolean result = false;
for(int j=0; j<validChars.length; j++){
if(password.charAt(i) == validChars[j]){
result = true;
break;
}
}
if(!result){
return false;
}
}
return true;
}
}