-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncDec.java
More file actions
176 lines (145 loc) · 4.8 KB
/
EncDec.java
File metadata and controls
176 lines (145 loc) · 4.8 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
import java.util.Scanner;
import java.io.*;
public class EncDec {
public static void main(String[] args) {
String action = "enc";
String algorithm = "shift";
String text = "";
String output = "";
String pathOut = "";
boolean hasData = false;
boolean hasOut = false;
int key = 0;
Alghorithm alg;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-mode") && args[i + 1].equals("dec")) {
action = "dec";
}
if (args[i].equals("-key")) {
key = Integer.parseInt(args[i + 1]);
if (key < 0) {
key = 0;
}
}
if (args[i].equals("-data")) {
hasData = true;
text = args[i + 1];
}
if (args[i].equals("-alg") && args[i + 1].equals("unicode")) {
algorithm = "unicode";
}
if (args[i].equals("-in") && !hasData) {
File file = new File("./" + args[i + 1]);
try (Scanner scanner = new Scanner(file)) {
text = scanner.nextLine();
} catch (Exception e) {
System.out.println("Error: input file not found.");
}
}
if (args[i].equals("-out")) {
hasOut = true;
pathOut = args[i + 1];
}
}
switch (algorithm) {
case "unicode" :
alg = new UnicodeAlghorithm(text, key);
break;
default :
alg = new ShiftAlghorithm(text, key);
break;
}
switch (action) {
case "enc" :
output = alg.encrypt();
break;
case "dec" :
output = alg.decrypt();
break;
}
if (hasOut) {
File file = new File("./" + pathOut);
try (PrintWriter printWriter = new PrintWriter(file)) {
printWriter.println(output);
} catch (Exception e) {
System.out.println("Error: output file not found.");
}
}
else {
System.out.println(output);
}
}
}
abstract class Alghorithm {
static char[] textArray;
static int key;
Alghorithm(String text, int key) {
this.textArray = text.toCharArray();
this.key = key;
}
abstract String encrypt();
abstract String decrypt();
}
class ShiftAlghorithm extends Alghorithm {
String alphabet = "abcdefghijklmnopqrstuvwxyz";
ShiftAlghorithm(String text, int key) {
super(text, key);
}
public String encrypt() {
StringBuilder encryptedText = new StringBuilder();
for (char letter : textArray) {
if (letter >= 65 && letter <= 90) {
encryptedText.append(alphabet.toUpperCase().charAt((letter + key - 65) % 26));
}
if (letter >= 97 && letter <= 122) {
encryptedText.append(alphabet.charAt((letter + key - 97) % 26));
}
else {
encryptedText.append(letter);
}
}
return encryptedText.toString();
}
public String decrypt() {
StringBuilder encryptedText = new StringBuilder();
for (char letter : textArray) {
if (letter >= 65 && letter <= 90) {
if (key > letter - 65) {
letter += 26;
}
encryptedText.append(alphabet.toUpperCase().charAt((letter - key - 65) % 25));
}
if (letter >= 97 && letter <= 122) {
if (key > letter - 97) {
letter += 26;
}
encryptedText.append(alphabet.charAt((letter - key - 97) % 25));
}
else {
encryptedText.append(letter);
}
}
return encryptedText.toString();
}
}
class UnicodeAlghorithm extends Alghorithm {
UnicodeAlghorithm(String text, int key) {
super(text, key);
}
public String encrypt() {
StringBuilder encryptedText = new StringBuilder();
for (char letter : textArray) {
letter += key;
encryptedText.append(letter);
}
return encryptedText.toString();
}
public String decrypt() {
StringBuilder decryptedText = new StringBuilder();
for (char letter : textArray) {
letter -= key;
decryptedText.append(letter);
}
return decryptedText.toString();
}
}