-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.java
202 lines (170 loc) · 6.73 KB
/
Index.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
198
199
200
201
202
import java.util.InputMismatchException;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.*;
import Ciphers.DES_P.HexEntries;
import Ciphers.Caesar;
import Ciphers.Substitution;
public class Index {
public static void main(String[] args) {
int option = -1;
int cipherOption = -1;
int todo = -1;
String key = "";
String myInput = "";
String text;
Scanner sc = new Scanner(System.in);
/* =============================================== */
System.out.println("Choose how you provide input");
System.out.println("\t1) console");
System.out.println("\t2) file input");
System.out.println("\t3) exit");
System.out.print("Enter your choice : ");
option = intInput(sc);
switch (option) {
case 1:
myInput = ConsoleInput(sc);
break;
case 2:
System.out.print("\nEnter the file name : ");
String file = sc.next();
myInput = FileInput(file);
break;
default:
System.out.println("Invalid input, exiting");
}
// myInput stores ASCII input if "encryption"
// myInput stores HEXA input if "decryption"
/* =============================================== */
System.out.println("\nChoose which cipher do you want to use");
System.out.println("\t1) substitution cipher");
System.out.println("\t2) caesar cipher");
System.out.println("\t3) DES cipher");
System.out.println("\t4) exit");
System.out.print("Enter your choice : ");
cipherOption = intInput(sc);
/* =============================================== */
System.out.println("\nHold up, what do you want to do though?");
System.out.println("\t1) encrypt");
System.out.println("\t2) decrypt");
System.out.println("\t3) exit");
System.out.print("Enter your choice : ");
todo = intInput(sc);
/* =============================================== */
if (todo == 1) {
switch (cipherOption) {
case 1:
// code for encrypting using Substitution cipher
text = Substitution.encrypted(myInput);
System.out.println("The encrypted string: " + text);
writeToFile(text);
break;
case 2:
// code for encrypting using Caesar cipher
System.out.print("\nEnter the shift value: ");
int shift = intInput(sc);
text = Caesar.encrypted(myInput, shift);
System.out.println("The encrypted string: " + text);
writeToFile(text);
break;
case 3:
text = HexEntries.asciiToHex(myInput);
String input16[] = split(text);
text = DES.Cryption(input16, 0);
System.out.println("The encrypted string (hexadecimal): " + text);
writeToFile(text);
break;
default:
System.out.println("Invalid cipher choice, exiting\n");
System.exit(2);
}
} else if (todo == 2) {
switch (cipherOption) {
case 1:
// code for decrypting using Substitution cipher
text = Substitution.decrypted(myInput);
System.out.println("The decrypted string: " + text);
break;
case 2:
// code for decrypting using Substitution cipher
System.out.print("\nEnter the shift value: ");
int shift = intInput(sc);
text = Caesar.decrypted(myInput, shift);
System.out.println("The decrypted string: " + text);
break;
case 3:
text = myInput;
String input16[] = split(text);
// key is hexadecimal
System.out.print("\nEnter key : ");
key = sc.next();
if (key.length() != 16) {
System.out.println("Please recheck your key");
break;
}
text = DES.Cryption(input16, 1, key);
System.out.println("\nThe decrypted string is : " + HexEntries.hexToAscii(text));
break;
default:
System.out.println("Invalid cipher input, exiting\n");
System.exit(2);
}
}
System.out.println("\nThank you for using our application\n");
}
static String ConsoleInput(Scanner sc) {
// Space words accepted
sc.nextLine();
System.out.print("\nEnter the text : ");
String str = sc.nextLine();
System.out.println("\nThe input string is : " + str);
return str;
}
static void writeToFile(String str) {
try {
FileWriter myWriter = new FileWriter("./test/output.txt");
myWriter.write(str);
myWriter.close();
} catch (IOException e) {
System.out.println("An error occurred while writing output to file.");
e.printStackTrace();
}
}
static int intInput(Scanner sc) {
int temp = -1;
try {
if (sc.hasNextInt())
temp = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("\n\tChoose from only the given options, please!");
} catch (Exception e) {
System.out.println("\n\tUnknown error reported, check Input");
}
return temp;
}
static String FileInput(String fileName) {
String str = "";
try {
FileReader input = new FileReader(fileName);
int i;
while ((i = input.read()) != -1)
str += (char) i;
input.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("\nThe input string is :" + str);
return str.replaceAll("\n", " ");
}
static String[] split(String input) {
// input is hexadecimal
// splitting input into length of 16
String input16[] = input.split("(?<=\\G.{16})");
int n = input16.length;
if (input16[n - 1].length() < 16) {
input16[n - 1] = String.format("%-" + 16 + "s", input16[n - 1]).replace(' ', '0');
}
// System.out.println(Arrays.toString(input16));
return input16;
}
}