-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInterface.java
More file actions
97 lines (86 loc) · 3.78 KB
/
UserInterface.java
File metadata and controls
97 lines (86 loc) · 3.78 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
import java.util.Scanner;
public class UserInterface {
private final Scanner scanner = new Scanner(System.in);
private final EncryptionService encryptionService = new EncryptionService();
// ANSI escape codes
public static final String RESET = "\u001B[0m"; // Reset color
public static final String BLACK = "\u001B[30m";
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String MAGENTA = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String WHITE = "\u001B[37m";
public void start() {
boolean continueRunning = true;
clearScreen();
System.out.println(GREEN + "Welcome to Encryptora!" + RESET);
while (continueRunning) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
handleEncryption();
break;
case 2:
handleDecryption();
break;
case 3:
continueRunning = false;
System.out.println(RED + "\nExiting Encryptora. Goodbye!" + RESET);
break;
default:
System.out.println(YELLOW + "\nInvalid choice. Please try again." + RESET);
}
}
scanner.close();
}
public static void clearScreen() {
try {
if (System.getProperty("os.name").contains("Windows")) {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
} else {
System.out.print("\033[H\033[2J");
System.out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void displayMenu() {
System.out.println(CYAN + "\nChoose an option:" + RESET);
System.out.println(BLUE + "1. Encrypt a file" + RESET);
System.out.println(BLUE + "2. Decrypt a file" + RESET);
System.out.println(RED + "3. Exit" + RESET);
}
private void handleEncryption() {
System.out.print(YELLOW + "\nEnter file path: " + RESET);
String inputFilePath = scanner.nextLine();
System.out.print(YELLOW + "\nEnter output file path: " + RESET);
String outputFilePath = scanner.nextLine();
System.out.print(YELLOW + "\nEnter password (16 characters for AES): " + RESET);
String password = scanner.nextLine();
try {
encryptionService.encrypt(inputFilePath, outputFilePath, password);
System.out.println(GREEN + "\nFile encrypted successfully!\n" + RESET);
} catch (Exception e) {
System.err.println(RED + "\nError: " + e.getMessage() + RESET);
}
}
private void handleDecryption() {
System.out.print(YELLOW + "\nEnter file path: " + RESET);
String inputDecryptPath = scanner.nextLine();
System.out.print(YELLOW + "\nEnter output file path: " + RESET);
String outputDecryptPath = scanner.nextLine();
System.out.print(YELLOW + "\nEnter password (16 characters for AES): " + RESET);
String decryptPassword = scanner.nextLine();
try {
encryptionService.decrypt(inputDecryptPath, outputDecryptPath, decryptPassword);
System.out.println(GREEN + "\nFile decrypted successfully!\n" + RESET);
} catch (Exception e) {
System.err.println(RED + "\nError: " + e.getMessage() + RESET);
}
}
}