-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.java
More file actions
277 lines (229 loc) · 7.85 KB
/
Menu.java
File metadata and controls
277 lines (229 loc) · 7.85 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SealedObject;
import javax.crypto.spec.SecretKeySpec;
public class Menu {
private boolean auth = false;
private User user;
private SecretKeySpec key;
public static void main(String[] args) {
Menu menu = new Menu();
clearScreen();
clearScreen();
while (!menu.auth) {
menu.auth = menu.authenticate();
if (!menu.auth) {
clearScreen();
System.out.println("Invalid Password\n");
}
} // end while
clearScreen();
menu.user.checkPassAge();
menu.mainMenu();
menu.writeData();
} // end main
public Menu() {
try {
Cipher cipher = Cipher.getInstance("AES");
// get the key
FileInputStream keyFile = new FileInputStream("key.bin");
ObjectInputStream keyObj = new ObjectInputStream(keyFile);
this.key = (SecretKeySpec) keyObj.readObject();
keyFile.close();
keyObj.close();
// unencrypt user file
cipher.init(Cipher.DECRYPT_MODE, this.key);
FileInputStream userFile = new FileInputStream("user.enc");
CipherInputStream cis = new CipherInputStream(userFile, cipher);
ObjectInputStream userObj = new ObjectInputStream(cis);
SealedObject sealedObject = (SealedObject) userObj.readObject();
this.user = (User) sealedObject.getObject(cipher);
userFile.close();
cis.close();
userObj.close();
} catch (Exception e) {
// no data, create key and new user
this.createUser();
// create key
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
byte[] keyBytes = keyGen.generateKey().getEncoded();
this.key = new SecretKeySpec(keyBytes, "AES");
FileOutputStream keyFileOut = new FileOutputStream("key.bin");
ObjectOutputStream keyObjOut = new ObjectOutputStream(keyFileOut);
keyObjOut.writeObject(this.key);
keyObjOut.close();
keyFileOut.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
// TODO write to error log
} // end try catch
} // end try catch
}// end constructor
public boolean authenticate() {
char[] responseArr = System.console().readPassword("Enter Master Password:\n");
String response = new String(responseArr);
return this.user.checkPassword(response);
} // end authenticate
public void writeData() {
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, this.key);
SealedObject sealedObject = new SealedObject(this.user, cipher);
FileOutputStream outFile = new FileOutputStream("user.enc");
CipherOutputStream cos = new CipherOutputStream(outFile, cipher);
ObjectOutputStream outObj = new ObjectOutputStream(cos);
outObj.writeObject(sealedObject);
outObj.close();
cos.close();
outFile.close();
} catch (Exception e) {
System.out.println("Error: Something went wrong while saving data");
// TODO write to error log
} // end try catch
} // end writeData
public void mainMenu() {
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
while (keepGoing) {
this.printMainMenu();
System.out.println("\nWhat would you like to do");
String response = scanner.nextLine();
if (response.equals("1")) {
clearScreen();
if (this.user.getTotalAccounts() > 0) {
this.user.listAccounts();
} else {
System.out.println("You do not have any accounts\n");
}
} else if (response.equals("2")) {
clearScreen();
if (this.user.getTotalAccounts() > 0) {
this.user.setCurrentAcct();
clearScreen();
this.AcctMenu();
clearScreen();
} else {
System.out.println("You do not have any accounts\n");
}
} else if (response.equals("3")) {
clearScreen();
this.user.createAccount();
clearScreen();
} else if (response.equals("4")) {
clearScreen();
if (this.user.getTotalAccounts() > 0) {
this.user.deleteAccount();
clearScreen();
} else {
System.out.println("You do not have any accounts\n");
}
} else if (response.equals("5")) {
clearScreen();
this.user.setPassword();
clearScreen();
} else if (response.equals("6")) {
clearScreen();
this.user.generatePass();
clearScreen();
} else if (response.equals("0")) {
clearScreen();
keepGoing = false;
} else {
clearScreen();
System.out.println("Error: Invalid Input\n");
} // end if else
} // end while
} // end mainMenu
public void printMainMenu() {
String mainMenu = "Main Menu\n";
mainMenu += "\n1) List Accounts";
mainMenu += "\n2) Select Account";
mainMenu += "\n3) Create Account";
mainMenu += "\n4) Delete Account";
mainMenu += "\n5) Change Master Password";
mainMenu += "\n6) Generate New Master Password";
mainMenu += "\n\n0) Exit";
System.out.println(mainMenu);
} // end printMainMenu
public void AcctMenu() {
Scanner scanner = new Scanner(System.in);
boolean keepGoing = true;
while (keepGoing) {
this.printAcctMenu();
System.out.println("\nWhat would you like to do");
String response = scanner.nextLine();
if (response.equals("1")) {
clearScreen();
System.out.println("Password: " + this.user.getAccountPass());
} else if (response.equals("2")) {
clearScreen();
this.user.setAccountPass();
clearScreen();
} else if (response.equals("3")) {
clearScreen();
this.user.generateAccountPass();
clearScreen();
} else if (response.equals("4")) {
clearScreen();
this.user.setAccountName();
clearScreen();
} else if (response.equals("5")) {
clearScreen();
this.user.setAccountURL();
clearScreen();
} else if (response.equals("0")) {
clearScreen();
keepGoing = false;
} else {
clearScreen();
System.out.println("Error: Invalid Input\n");
} // end if else
} // end while
} // end AcctMenu
public void printAcctMenu() {
System.out.println("Account Name: " + this.user.getAccountName());
System.out.println("Account URL: " + this.user.getAccountURL());
String acctMenu = "\nAccount Menu\n";
acctMenu += "\n1) View Password";
acctMenu += "\n2) Change Password";
acctMenu += "\n3) Generate New Password";
acctMenu += "\n4) Change Account Name";
acctMenu += "\n5) Change Account URL";
acctMenu += "\n\n0) Return to Main Menu";
System.out.println(acctMenu);
} // end printAcctMenu
public void createUser() {
clearScreen();
String pass = "";
System.out.println("Creating new account");
boolean keepGoing = true;
while (keepGoing) {
char[] firstEntry = System.console().readPassword("\nEnter Password:\n");
char[] secondEntry = System.console().readPassword("Retype Password:\n");
if (Arrays.equals(firstEntry, secondEntry)) {
pass = new String(firstEntry);
keepGoing = false;
} else {
System.out.println("\nError: Passwords do not match");
} // end if else
} // end while
this.user = new User(pass);
this.auth = true;
} // end createUser
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
} // end clearScreen
} // end menu