|
@@ -1,16 +1,26 @@
|
|
1
|
+import java.io.IOException;
|
|
2
|
+import java.nio.file.*;
|
|
3
|
+import java.util.*;
|
|
4
|
+
|
1
|
5
|
public class ROT13 {
|
2
|
6
|
private String cryptKey;
|
3
|
7
|
private String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
8
|
+ private String sonnet = "/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.txt";
|
|
9
|
+ private String cryptSonnet = "/Users/jaredn/Labs/week5/ZCW-SimpleCrypt0/sonnet18.enc";
|
4
|
10
|
|
5
|
11
|
|
6
|
12
|
ROT13(Character cs, Character cf) {
|
7
|
|
- int index = Math.abs((int)cs - (int)cf);
|
8
|
|
- cryptKey = rotate(alphabet, alphabet.charAt(index));
|
|
13
|
+ cryptKey = rotate(alphabet, alphabet.charAt(Math.abs((int)cs - (int)cf)));
|
9
|
14
|
}
|
10
|
15
|
|
11
|
16
|
ROT13(){ cryptKey = rotate(alphabet, 'n'); }
|
12
|
17
|
|
13
|
18
|
public String crypt(String text) throws UnsupportedOperationException {
|
|
19
|
+ return encrypt(text);
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ @SuppressWarnings("Duplicates")
|
|
23
|
+ public String encrypt(String text) {
|
14
|
24
|
StringBuilder result = new StringBuilder();
|
15
|
25
|
|
16
|
26
|
for(Character c : text.toCharArray()) {
|
|
@@ -25,15 +35,56 @@ public class ROT13 {
|
25
|
35
|
return result.toString();
|
26
|
36
|
}
|
27
|
37
|
|
28
|
|
- public String encrypt(String text) {
|
29
|
|
- return crypt(text);
|
30
|
|
- }
|
31
|
|
-
|
|
38
|
+ @SuppressWarnings("Duplicates")
|
32
|
39
|
public String decrypt(String text) {
|
33
|
|
- return crypt(text);
|
|
40
|
+ StringBuilder result = new StringBuilder();
|
|
41
|
+
|
|
42
|
+ for(Character c : text.toCharArray()) {
|
|
43
|
+ if (Character.isLetter(c)) {
|
|
44
|
+ if (Character.isUpperCase(c)) {
|
|
45
|
+ result.append(Character.toUpperCase(alphabet.charAt(cryptKey.indexOf(Character.toLowerCase(c)))));
|
|
46
|
+ }
|
|
47
|
+ else result.append(alphabet.charAt(cryptKey.indexOf(c)));
|
|
48
|
+ }
|
|
49
|
+ else result.append(c);
|
|
50
|
+ }
|
|
51
|
+ return result.toString();
|
34
|
52
|
}
|
35
|
53
|
|
36
|
54
|
public static String rotate(String s, Character c) {
|
37
|
55
|
return s.substring(s.indexOf(c)) + s.substring(0, s.indexOf(c));
|
38
|
56
|
}
|
|
57
|
+
|
|
58
|
+ public void encryptSonnet() throws IOException {
|
|
59
|
+ Files.write(Paths.get(cryptSonnet), encryptFile(sonnet).getBytes());
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ public String decryptSonnet() {
|
|
63
|
+ return decryptFile(cryptSonnet);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public String encryptFile(String filePath) {
|
|
67
|
+ try {
|
|
68
|
+ return encrypt(parseFile(filePath));
|
|
69
|
+ } catch (IOException e) {
|
|
70
|
+ System.out.println(e.getMessage());
|
|
71
|
+ }
|
|
72
|
+ return null;
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public String decryptFile(String encryptedFilePath) {
|
|
76
|
+ try {
|
|
77
|
+ return decrypt(parseFile(encryptedFilePath));
|
|
78
|
+ } catch (IOException e) {
|
|
79
|
+ System.out.println(e.getMessage());
|
|
80
|
+ }
|
|
81
|
+ return null;
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ public String parseFile(String filePath) throws IOException {
|
|
85
|
+ List<String> content = Files.readAllLines(Paths.get(filePath));
|
|
86
|
+ StringBuilder result = new StringBuilder();
|
|
87
|
+ content.forEach(e -> result.append(e).append("\n"));
|
|
88
|
+ return result.toString();
|
|
89
|
+ }
|
39
|
90
|
}
|