|
@@ -1,3 +1,7 @@
|
|
1
|
+import javax.security.sasl.SaslServer;
|
|
2
|
+import java.io.*;
|
|
3
|
+import java.nio.file.Files;
|
|
4
|
+
|
1
|
5
|
import static java.lang.Character.isLowerCase;
|
2
|
6
|
import static java.lang.Character.isUpperCase;
|
3
|
7
|
import static java.lang.Character.toLowerCase;
|
|
@@ -11,7 +15,7 @@ public class ROT13 {
|
11
|
15
|
|
12
|
16
|
ROT13(Character cs, Character cf) {
|
13
|
17
|
rotateBy = numbersByCount(cs, cf);
|
14
|
|
- rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz", cf).toCharArray();
|
|
18
|
+ rotatedAlphbet = rotate("abcdefghijklmnopqrstuvwxyz").toCharArray();
|
15
|
19
|
}
|
16
|
20
|
|
17
|
21
|
ROT13() {}
|
|
@@ -37,7 +41,9 @@ public class ROT13 {
|
37
|
41
|
}
|
38
|
42
|
|
39
|
43
|
public static void main(String[] args){
|
40
|
|
- new ROT13('a','n');
|
|
44
|
+ ROT13 rot13 = new ROT13('a','n');
|
|
45
|
+ rot13.encryptFile("sonnet18.txt");
|
|
46
|
+ rot13.decryptFile("sonnet18.enc");
|
41
|
47
|
//System.out.println(new ROT13().crypt("Gb trg gb gur bgure fvqr!"));
|
42
|
48
|
}
|
43
|
49
|
|
|
@@ -55,7 +61,6 @@ public class ROT13 {
|
55
|
61
|
} else{
|
56
|
62
|
sb.append(car);
|
57
|
63
|
}
|
58
|
|
-
|
59
|
64
|
}
|
60
|
65
|
return sb.toString();
|
61
|
66
|
|
|
@@ -78,7 +83,40 @@ public class ROT13 {
|
78
|
83
|
|
79
|
84
|
public String decrypt(String text) {
|
80
|
85
|
|
81
|
|
- return this.crypt(text);
|
|
86
|
+ char[] cars = text.toCharArray();
|
|
87
|
+
|
|
88
|
+ StringBuilder sb = new StringBuilder();
|
|
89
|
+
|
|
90
|
+ for(int i = 0; i < cars.length; i++){
|
|
91
|
+ if(Character.isLetter(cars[i])){
|
|
92
|
+ sb.append(cars[i]);
|
|
93
|
+ if(rotateBy - i < 0){
|
|
94
|
+ System.out.println(rotateBy -1);
|
|
95
|
+ sb.append(rotatedAlphbet[(rotateBy - i) + (rotatedAlphbet.length -1)]);
|
|
96
|
+ } else {
|
|
97
|
+ sb.append(rotatedAlphbet[rotateBy - i]);
|
|
98
|
+ }
|
|
99
|
+ } else {
|
|
100
|
+ sb.append(cars[i]);
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+ return sb.toString();
|
|
104
|
+ }
|
|
105
|
+
|
|
106
|
+ public String rotate(String s) {
|
|
107
|
+ char[] cars = s.toCharArray();
|
|
108
|
+
|
|
109
|
+ int looper = rotateBy;
|
|
110
|
+ char temp;
|
|
111
|
+ while(looper != 0){
|
|
112
|
+ temp = cars[0];
|
|
113
|
+ for(int i = 0; i < cars.length -1; i++){
|
|
114
|
+ cars[i] = cars[i +1];
|
|
115
|
+ }
|
|
116
|
+ cars[cars.length -1] = temp;
|
|
117
|
+ looper--;
|
|
118
|
+ }
|
|
119
|
+ return new String(cars);
|
82
|
120
|
}
|
83
|
121
|
|
84
|
122
|
public static String rotate(String s, Character c) {
|
|
@@ -96,6 +134,66 @@ public class ROT13 {
|
96
|
134
|
return new String(cars);
|
97
|
135
|
}
|
98
|
136
|
|
|
137
|
+ public void encryptFile(String fileName){
|
|
138
|
+ try{
|
|
139
|
+ BufferedReader br = new BufferedReader(new FileReader(fileName));
|
|
140
|
+ StringBuilder sb = new StringBuilder();
|
|
141
|
+ String line = br.readLine();
|
|
142
|
+
|
|
143
|
+ while(line != null){
|
|
144
|
+ sb.append(line);
|
|
145
|
+ sb.append("\n");
|
|
146
|
+ line = br.readLine();
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ String encryptedFile = this.crypt(sb.toString());
|
|
150
|
+
|
|
151
|
+ try {
|
|
152
|
+ File file = new File("sonnet18.enc");
|
|
153
|
+ FileWriter fileWriter = new FileWriter(file);
|
|
154
|
+ fileWriter.write(encryptedFile);
|
|
155
|
+ fileWriter.flush();
|
|
156
|
+ fileWriter.close();
|
|
157
|
+ } catch (IOException e) {
|
|
158
|
+ e.printStackTrace();
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ System.out.println(encryptedFile);
|
|
162
|
+
|
|
163
|
+ } catch (IOException e) {
|
|
164
|
+ e.printStackTrace();
|
|
165
|
+ }
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ public void decryptFile(String fileName){
|
|
169
|
+ try{
|
|
170
|
+ BufferedReader br = new BufferedReader(new FileReader(fileName));
|
|
171
|
+ StringBuilder sb = new StringBuilder();
|
|
172
|
+ String line = br.readLine();
|
|
173
|
+
|
|
174
|
+ while(line != null){
|
|
175
|
+ sb.append(line);
|
|
176
|
+ sb.append("\n");
|
|
177
|
+ line = br.readLine();
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ String decryptFile = this.decrypt(sb.toString());
|
99
|
181
|
|
|
182
|
+ try {
|
|
183
|
+ File file = new File("sonnet18dec.txt");
|
|
184
|
+ FileWriter fileWriter = new FileWriter(file);
|
|
185
|
+ fileWriter.write(decryptFile);
|
|
186
|
+ fileWriter.flush();
|
|
187
|
+ fileWriter.close();
|
|
188
|
+ } catch (IOException e) {
|
|
189
|
+ e.printStackTrace();
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ System.out.println(decryptFile);
|
|
193
|
+
|
|
194
|
+ } catch (IOException e) {
|
|
195
|
+ e.printStackTrace();
|
|
196
|
+ }
|
|
197
|
+ }
|
100
|
198
|
|
101
|
199
|
}
|