-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunEncoder.java
197 lines (182 loc) · 7.77 KB
/
RunEncoder.java
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
/***********************************
* Filename: RunEncoder
* Author: Brandon Badraoui
* Collaborators:
* Created: 3/25/2024
* Modified: 4/14/24
* Purpose: Facilitates the running of the encoder program. Acts as an application class
* <p>
* Attributes:
* -uiCheck: String
* -userInput: String
* -selectedCipher: int
* -typeImport: int
* <p>
* Methods:
* +main(String[] args): void
* -runType1(int): void
* -runType2(int): void
* -typeStringCheck(): void
* -typeString(): void
* -getCipherUserInput(String): int
* -getTypeImport(String): int
* +getUserInput(): String
* +setTypeImport(int): void
***********************************/
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class RunEncoder {
private String uiCheck = "N";
private String userInput = "";
private int selectedCipher;
private int typeImport;
public static void main(String[] args) {
int runAgain = 0;
RunEncoder run = new RunEncoder();
do {
run.selectedCipher = run.getCipherUserInput("\nHello, welcome to the Badraoui Encryption Software!\nWhat Cipher would you like to select?\n1.Caesar Cipher (Weak)\n2.XOR Cipher (Moderate)\n3.Hill Cipher (Strong)");
run.setTypeImport(run.getTypeImport("\nWould you like to type a string or import a file?\n1.Type a string\n2.Import a file"));
if (run.typeImport == 1) {
run.runType1(run.selectedCipher);
} else if (run.typeImport == 2) {
run.runType2(run.selectedCipher);
}
runAgain = run.checkRunAgain("Would you like to encrypt again? (Type either 1 or 2)\n1.No\n2.Yes");
}
while (runAgain == 2);
}
private void runType1(int selectedCipher) {
Path path = Paths.get("EncryptedString.txt");
Alphabet al = new Alphabet();
CaesarCipher cc = new CaesarCipher();
XORCipher xc = new XORCipher();
HillCipher hc = new HillCipher();
do {
typeString();
typeStringCheck();
} while (uiCheck.equals("N"));
if (selectedCipher == 1) {
al.runAlpha(typeImport, getUserInput());
cc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(cc.getCcCipherInt());
System.out.print("Encrypted String (Caesar): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
if (selectedCipher == 2) {
al.runAlpha(typeImport, getUserInput());
xc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(xc.getXCipherInt());
System.out.print("Encrypted String (XOR): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
if (selectedCipher == 3) {
al.runAlpha(typeImport, getUserInput());
hc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(hc.getHCipher());
System.out.print("Encrypted String (Hill): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
System.out.println("Your file is located at: " + path.toAbsolutePath() + "\n");
}
private void runType2(int selectedCipher) {
Path path = Paths.get("EncryptedString.txt");
Alphabet al = new Alphabet();
CaesarCipher cc = new CaesarCipher();
XORCipher xc = new XORCipher();
HillCipher hc = new HillCipher();
if (selectedCipher == 1) {
al.runAlpha(typeImport, "");
cc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(cc.getCcCipherInt());
System.out.print("Encrypted String (Caesar): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
if (selectedCipher == 2) {
al.runAlpha(typeImport, "");
xc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(xc.getXCipherInt());
System.out.print("Encrypted String (XOR): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
if (selectedCipher == 3) {
al.runAlpha(typeImport, "");
hc.runCipher(al.getStringAsNum());
char[] CipheredText = al.numToLetter(hc.getHCipher());
System.out.print("Encrypted String (Hill): ");
System.out.println(CipheredText);
al.outputToFile("EncryptedString.txt", CipheredText);
}
System.out.println("Your file is located at: " + path.toAbsolutePath() + "\n");
}
private int checkRunAgain(String prompt) {
int runAgain = -1;
try {
System.out.println(prompt);
Scanner sc = new Scanner(System.in);
runAgain = sc.nextInt();
if (runAgain > 2 || runAgain <= 0) {
prompt = "Would you like to encrypt again? (Type either 1 or 2)\n1.No\n2.Yes";
runAgain = -1;
}
} catch (Exception e) {
prompt = "Incorrect input, please try again.\nWould you like to encrypt again? (Type either 1 or 2) \n1.No \n2.Yes";
}
return runAgain;
}
private void typeStringCheck() {
Scanner sc = new Scanner(System.in);
System.out.println("Is this what you typed? " + "(" + userInput + ")" + "\nIf yes, type Y. If no, type N");
uiCheck = sc.nextLine().toUpperCase();
}
private void typeString() {
Scanner sc = new Scanner(System.in);
System.out.println("Please type your string. (Please keep in mind, all symbols will be removed)");
userInput = sc.nextLine().toLowerCase().replaceAll("[^a-zA-Z0-9_-]", " ");
}
private int getCipherUserInput(String prompt) {
int ciphSelect = -1;
while (ciphSelect < 0) {
try {
System.out.println(prompt);
Scanner sc = new Scanner(System.in);
ciphSelect = sc.nextInt();
if (ciphSelect > 3 || ciphSelect <= 0) {
prompt = "Incorrect input, please try again. \nWhat Cipher would you like to select?\n1.Caesar Cipher (Weak)\n2.XOR Cipher (Moderate)\n3.Hill Cipher (Strong)";
ciphSelect = -1;
}
} catch (Exception e) {
prompt = "Incorrect input, please try again. \nWhat Cipher would you like to select?\n1.Caesar Cipher (Weak)\n2.XOR Cipher (Moderate)\n3.Hill Cipher (Strong)";
}
}
return ciphSelect;
}
private int getTypeImport(String prompt) {
int typeSelect = -1;
while (typeSelect < 0) {
try {
System.out.println(prompt);
Scanner sc = new Scanner(System.in);
typeSelect = sc.nextInt();
if (typeSelect > 2 || typeSelect <= 0) {
prompt = "Incorrect input, please try again.\nWould you like to type a string or import a file?\n1.Type a string\n2.Import a file";
typeSelect = -1;
}
} catch (Exception e) {
prompt = "Incorrect input, please try again.\nWould you like to type a string or import a file?\n1.Type a string\n2.Import a file";
}
}
return typeSelect;
}
public String getUserInput() {
return userInput;
}
public void setTypeImport(int typeImport) {
this.typeImport = typeImport;
}
}