|
|
@@ -1,6 +1,5 @@
|
|
1
|
|
-import java.util.Arrays;
|
|
2
|
|
-import java.util.List;
|
|
3
|
|
-import java.util.Map;
|
|
|
1
|
+import java.io.*;
|
|
|
2
|
+import java.util.*;
|
|
4
|
3
|
import java.util.stream.Stream;
|
|
5
|
4
|
|
|
6
|
5
|
import static java.lang.Character.isLowerCase;
|
|
|
@@ -12,14 +11,17 @@ public class ROT13 {
|
|
12
|
11
|
Character original;
|
|
13
|
12
|
Character adjusted;
|
|
14
|
13
|
int shift = 13;
|
|
15
|
|
- String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
16
|
|
- String shifted = "NOPQRSTUVWXYZABCDEFGHIJKLM";
|
|
|
14
|
+ String alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
15
|
+ String shiftedUpper = "NOPQRSTUVWXYZABCDEFGHIJKLM";
|
|
|
16
|
+ String alphaLower = "abcdefghijklmnopqrstuvwxyz";
|
|
|
17
|
+ String shiftedLower = "nopqrstuvwxyzabcdefghijklm";
|
|
17
|
18
|
|
|
18
|
19
|
ROT13(Character cs, Character cf) {
|
|
19
|
20
|
this.original = cs;
|
|
20
|
21
|
this.adjusted = cf;
|
|
21
|
22
|
this.shift = cf - cs;
|
|
22
|
|
- this.shifted = rotate(alpha, cf);
|
|
|
23
|
+ this.shiftedUpper = rotate(alphaUpper, Character.toUpperCase(cf));
|
|
|
24
|
+ this.shiftedLower = rotate(alphaLower, Character.toLowerCase(cf));
|
|
23
|
25
|
|
|
24
|
26
|
}
|
|
25
|
27
|
|
|
|
@@ -27,10 +29,13 @@ public class ROT13 {
|
|
27
|
29
|
|
|
28
|
30
|
}
|
|
29
|
31
|
|
|
30
|
|
- public String getShifted() {
|
|
31
|
|
- return shifted;
|
|
|
32
|
+ public String getShiftedUpper() {
|
|
|
33
|
+ return shiftedUpper;
|
|
32
|
34
|
}
|
|
33
|
35
|
|
|
|
36
|
+ public String getShiftedLower() {
|
|
|
37
|
+ return shiftedLower;
|
|
|
38
|
+ }
|
|
34
|
39
|
|
|
35
|
40
|
public String crypt(String text) throws UnsupportedOperationException {
|
|
36
|
41
|
|
|
|
@@ -39,46 +44,57 @@ public class ROT13 {
|
|
39
|
44
|
|
|
40
|
45
|
public String encrypt(String text) {
|
|
41
|
46
|
String result = "";
|
|
42
|
|
- text = text.toUpperCase();
|
|
43
|
47
|
for (Character c: text.toCharArray()) {
|
|
44
|
48
|
if(Character.isLetter(c)) {
|
|
45
|
|
- for (int i = 0; i < alpha.length(); i++) {
|
|
46
|
|
- if(c.equals(alpha.charAt(i))) {
|
|
47
|
|
- result += getShifted().charAt(i);
|
|
|
49
|
+ String alphabet = alphaLower;
|
|
|
50
|
+ String shifted = this.shiftedLower;
|
|
|
51
|
+ if(Character.isUpperCase(c)) {
|
|
|
52
|
+ alphabet = alphaUpper;
|
|
|
53
|
+ shifted = this.shiftedUpper;
|
|
|
54
|
+ }
|
|
|
55
|
+ for (int i = 0; i < alphabet.length(); i++) {
|
|
|
56
|
+ if(c.equals(alphabet.charAt(i))) {
|
|
|
57
|
+ result += shifted.charAt(i);
|
|
48
|
58
|
}
|
|
49
|
59
|
}
|
|
|
60
|
+
|
|
50
|
61
|
} else {
|
|
51
|
62
|
result += c;
|
|
52
|
63
|
}
|
|
53
|
64
|
|
|
54
|
65
|
}
|
|
55
|
66
|
|
|
56
|
|
- return result.charAt(0) + result.substring(1).toLowerCase();
|
|
57
|
|
-
|
|
|
67
|
+ return result;
|
|
58
|
68
|
}
|
|
59
|
69
|
|
|
60
|
70
|
public String decrypt(String text) {
|
|
61
|
71
|
String result = "";
|
|
62
|
|
- text = text.toUpperCase();
|
|
|
72
|
+
|
|
63
|
73
|
for (Character c: text.toCharArray()) {
|
|
64
|
74
|
if(Character.isLetter(c)) {
|
|
65
|
|
- for (int i = 0; i < alpha.length(); i++) {
|
|
66
|
|
- if(c.equals(alpha.charAt(i))) {
|
|
67
|
|
- result += getShifted().charAt(i);
|
|
|
75
|
+ String alphabet = alphaLower;
|
|
|
76
|
+ String shifted = this.shiftedLower;
|
|
|
77
|
+ if(Character.isUpperCase(c)) {
|
|
|
78
|
+ alphabet = alphaUpper;
|
|
|
79
|
+ shifted = this.shiftedUpper;
|
|
|
80
|
+ }
|
|
|
81
|
+ for (int i = 0; i < alphabet.length(); i++) {
|
|
|
82
|
+ if(c.equals(alphabet.charAt(i))) {
|
|
|
83
|
+ result += shifted.charAt(i);
|
|
68
|
84
|
}
|
|
69
|
85
|
}
|
|
|
86
|
+
|
|
70
|
87
|
} else {
|
|
71
|
88
|
result += c;
|
|
72
|
89
|
}
|
|
73
|
90
|
|
|
74
|
91
|
}
|
|
75
|
92
|
|
|
76
|
|
- return result.charAt(0) + result.substring(1).toLowerCase();
|
|
|
93
|
+ return result;
|
|
77
|
94
|
}
|
|
78
|
95
|
|
|
79
|
96
|
public static String rotate(String s, Character c) {
|
|
80
|
97
|
|
|
81
|
|
- c = Character.toUpperCase(c);
|
|
82
|
98
|
int index = 0;
|
|
83
|
99
|
for (int i = 0; i < s.length(); i++) {
|
|
84
|
100
|
if(s.charAt(i) == c){
|
|
|
@@ -88,4 +104,40 @@ public class ROT13 {
|
|
88
|
104
|
return s.substring(index) + s.substring(0, index);
|
|
89
|
105
|
}
|
|
90
|
106
|
|
|
|
107
|
+ public void fileEncrypter(String fileName, String newFileName) throws FileNotFoundException {
|
|
|
108
|
+
|
|
|
109
|
+ String fileString = fileToString(fileName);
|
|
|
110
|
+ String newFileString = encrypt(fileString);
|
|
|
111
|
+ System.out.println(newFileString);
|
|
|
112
|
+
|
|
|
113
|
+ try {
|
|
|
114
|
+ File file = new File(newFileName);
|
|
|
115
|
+ FileWriter fileWriter = new FileWriter(file);
|
|
|
116
|
+ fileWriter.write(newFileString);
|
|
|
117
|
+ fileWriter.flush();
|
|
|
118
|
+ fileWriter.close();
|
|
|
119
|
+ } catch (IOException e) {
|
|
|
120
|
+ System.out.println("Error writing file");
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ public String fileToString(String fileName) throws FileNotFoundException {
|
|
|
126
|
+ File file = new File(fileName);
|
|
|
127
|
+ BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
|
128
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
129
|
+ String line = null;
|
|
|
130
|
+
|
|
|
131
|
+ try {
|
|
|
132
|
+ while((line = reader.readLine()) != null) {
|
|
|
133
|
+ stringBuilder.append(line + "\n");
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ } catch (Exception e) {
|
|
|
137
|
+ System.out.println("Error Reading File");
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ return stringBuilder.toString();
|
|
|
141
|
+ }
|
|
|
142
|
+
|
|
91
|
143
|
}
|