-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
158 lines (152 loc) · 5.7 KB
/
Main.java
File metadata and controls
158 lines (152 loc) · 5.7 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
package encryptdecrypt;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
// Abstract class ensures that all classes which extend CypherMethods contain a function to perform encoding or decoding
abstract class CypherMethods {
public abstract void algorithm(String alg, String data, String outFile, int key);
}
class Encoding extends CypherMethods {
// function returns input as an array of characters.
public char [] toArray(String data) {
return data.toCharArray();
}
// function returns an empty array of length arr.length
public char [] productArray(String data) {
char [] arr = toArray(data);
return new char[arr.length];
}
// Overrides the abstract base class' method to encoded a messages.
@Override
public void algorithm(String alg, String data, String outFile, int key) {
char [] messageArray = toArray(data);
char [] encArray = productArray(data);
int myShift = key % 26 + 26;
if (alg.equals("unicode")) {
for (int i = 0; i <= messageArray.length - 1; ++i) {
encArray[i] = (char) ((int) messageArray[i] + key);
}
} else {
for (int i = 0; i <= messageArray.length - 1; ++i) {
if (Character.isLetter(messageArray[i])) {
if (Character.isUpperCase(messageArray[i])) {
encArray[i] = (char) ('A' + (messageArray[i] - 'A' + myShift) % 26);
} else {
encArray[i] = (char) ('a' + (messageArray[i] - 'a' + myShift) % 26);
}
} else {
encArray[i] = messageArray[i];
}
}
}
Main.outputFile(outFile, encArray);
}
}
class Decoding extends CypherMethods {
public char [] toArray(String data) {
return data.toCharArray();
}
public char [] productArray(String data) {
char [] arr = toArray(data);
return new char[arr.length];
}
@Override
public void algorithm(String alg, String data, String outFile, int key) {
char [] decArray = toArray(data);
char [] messageArray = productArray(data);
int myShift = 26 - (key % 26);
if (alg.equals("unicode")) {
for (int i = 0; i <= decArray.length - 1; ++i) {
messageArray[i] = (char) ((int) decArray[i] - key);
}
} else {
for (int i = 0; i <= decArray.length - 1; ++i) {
if (Character.isLetter(decArray[i])) {
if (Character.isUpperCase(decArray[i])) {
messageArray[i] = (char) ('A' + (((decArray[i] - 'A') + myShift) % 26));
} else {
messageArray[i] = (char) ('a' + (((decArray[i] - 'a') + myShift) % 26));
}
} else {
messageArray[i] = decArray[i];
}
}
}
Main.outputFile(outFile, messageArray);
}
}
class AlgorithmApplicator {
private CypherMethods method;
public void setMethod(CypherMethods method) {
this.method = method;
}
public void algorithm(String alg, String data, String outFile, int key) {
this.method.algorithm(alg, data, outFile, key);
}
}
public class Main {
public static String readFileInput(String fileName) throws IOException {
return new String(Files.readAllBytes(Paths.get(fileName)));
}
public static void outputFile(String outputFile, char[] messageArray) {
if (!(outputFile.equals(""))) {
File file = new File(outputFile);
try(FileWriter writer = new FileWriter(file)) {
writer.write(new String(messageArray));
} catch (IOException e) {
System.out.printf("Error: %s", e.getMessage());
}
} else {
System.out.printf("%s", new String(messageArray));
}
}
public static void main(String[] args) {
String alg = "shift";
String mode = "enc";
String data = "";
String inFile = "";
String outFile = "";
int key = 0;
for (int i = 0; i <= args.length - 1; i += 2) {
switch (args[i]) {
case "-alg" -> {
if (args[i + 1].equals("unicode")) {
alg = args[i+1];
}
}
case "-mode" -> {
if ((args[i + 1].equals("enc")) || (args[i + 1].equals("dec"))) {
mode = args[i + 1];
}
}
case "-key" -> key = Integer.parseInt(args[i + 1]);
case "-data" -> data = args[i + 1];
case "-in" -> {
try {
inFile = readFileInput(args[i + 1]);
} catch (IOException e) {
System.out.printf("Error: %s", e.getMessage());
}
}
case "-out" -> outFile = args[i + 1];
default -> {
}
}
}
String eodData = data.equals("") ? inFile : data;
AlgorithmApplicator applicator = new AlgorithmApplicator();
switch (mode) {
case "enc" -> {
applicator.setMethod(new Encoding());
applicator.algorithm(alg, eodData, outFile, key);
}
case "dec" -> {
applicator.setMethod(new Decoding());
applicator.algorithm(alg, eodData, outFile, key);
}
default -> System.out.println("You entered an invalid response.");
}
}
}