|
@@ -1,32 +1,86 @@
|
1
|
|
-import static java.lang.Character.isLowerCase;
|
2
|
|
-import static java.lang.Character.isUpperCase;
|
3
|
|
-import static java.lang.Character.toLowerCase;
|
|
1
|
+import java.io.*;
|
|
2
|
+import java.nio.file.Files;
|
|
3
|
+import java.nio.file.Paths;
|
4
|
4
|
|
5
|
|
-public class ROT13 {
|
|
5
|
+import static java.lang.Character.*;
|
|
6
|
+
|
|
7
|
+public class ROT13 {
|
|
8
|
+ int difference;
|
6
|
9
|
|
7
|
10
|
ROT13(Character cs, Character cf) {
|
|
11
|
+ difference = (int) cf - (int) cs;
|
8
|
12
|
}
|
9
|
13
|
|
10
|
14
|
ROT13() {
|
|
15
|
+ difference = 13;
|
11
|
16
|
}
|
12
|
17
|
|
13
|
18
|
|
14
|
19
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
20
|
|
16
|
|
- return "";
|
|
21
|
+ StringBuffer result = new StringBuffer();
|
|
22
|
+
|
|
23
|
+ for (int i = 0; i < text.length(); i++) {
|
|
24
|
+ if (!Character.isLetter(text.charAt(i))) {
|
|
25
|
+ result.append(text.charAt(i));
|
|
26
|
+ } else if (Character.isUpperCase(text.charAt(i))) {
|
|
27
|
+ char ch = (char) (((int) text.charAt(i) + difference - 65) % 26 + 65);
|
|
28
|
+ result.append(ch);
|
|
29
|
+ } else {
|
|
30
|
+ char ch = (char) (((int) text.charAt(i) + difference - 97) % 26 + 97);
|
|
31
|
+ result.append(ch);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+ return result.toString();
|
17
|
35
|
}
|
18
|
36
|
|
19
|
|
- public String encrypt(String text) {
|
20
|
|
- return text;
|
|
37
|
+
|
|
38
|
+ public String encrypt(String text) {
|
|
39
|
+ return crypt(text);
|
21
|
40
|
}
|
22
|
41
|
|
23
|
42
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
43
|
+ return crypt(text);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ public String rotate(String s, Character c) {
|
|
47
|
+ int offset = (int) c - 65;
|
|
48
|
+ int i = offset % s.length();
|
|
49
|
+ return s.substring(i) + s.substring(0, i);
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ public String readFile(String filePath) throws IOException {
|
|
53
|
+ return new String(Files.readAllBytes(Paths.get(filePath)));
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+ }
|
|
57
|
+ public File encryptedFile() throws IOException {
|
|
58
|
+ File file = new File("sonnet18.enc");
|
|
59
|
+ FileWriter writer = new FileWriter(file);
|
|
60
|
+ writer.write(encrypt(readFile("sonnet18.txt")));
|
|
61
|
+ writer.close();
|
|
62
|
+
|
|
63
|
+ return file;
|
25
|
64
|
}
|
26
|
65
|
|
27
|
|
- public static String rotate(String s, Character c) {
|
28
|
66
|
|
29
|
|
- return "";
|
|
67
|
+
|
30
|
68
|
}
|
31
|
69
|
|
32
|
|
-}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|