|
@@ -1,32 +1,66 @@
|
1
|
1
|
import static java.lang.Character.isLowerCase;
|
2
|
2
|
import static java.lang.Character.isUpperCase;
|
3
|
3
|
import static java.lang.Character.toLowerCase;
|
|
4
|
+import java.util.*;
|
|
5
|
+import java.util.stream.*;
|
4
|
6
|
|
5
|
7
|
public class ROT13 {
|
|
8
|
+ int shift;
|
|
9
|
+ Map<Character, Character> cypher;
|
|
10
|
+ String alphabet = "abcdefghijklmnopqrstuvwxyz";
|
6
|
11
|
|
7
|
12
|
ROT13(Character cs, Character cf) {
|
|
13
|
+ shift = Math.abs((int)cs - (int)cf);
|
|
14
|
+ setupHM();
|
8
|
15
|
}
|
9
|
16
|
|
10
|
17
|
ROT13() {
|
|
18
|
+ shift = 13;
|
|
19
|
+ setupHM();
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ public void setupHM() {
|
|
23
|
+ cypher = new HashMap<>();
|
|
24
|
+ for(Character c : alphabet.toCharArray()) {
|
|
25
|
+ int offset = (int)c + shift > 122 ? (int)c - shift : (int)c + shift;
|
|
26
|
+ cypher.put(c, (char)offset);
|
|
27
|
+ }
|
11
|
28
|
}
|
12
|
29
|
|
13
|
30
|
|
14
|
31
|
public String crypt(String text) throws UnsupportedOperationException {
|
|
32
|
+ StringBuilder result = new StringBuilder();
|
15
|
33
|
|
16
|
|
- return "";
|
|
34
|
+ for(Character c : text.toCharArray()) {
|
|
35
|
+ if (Character.isLetter(c)) {
|
|
36
|
+ if (Character.isUpperCase(c)) {
|
|
37
|
+ result.append(Character.toUpperCase(cypher.get(Character.toLowerCase(c))));
|
|
38
|
+ }
|
|
39
|
+ else result.append(cypher.get(c));
|
|
40
|
+ }
|
|
41
|
+ else {
|
|
42
|
+ result.append(c);
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ return result.toString();
|
17
|
46
|
}
|
18
|
47
|
|
19
|
48
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
49
|
+ return crypt(text);
|
21
|
50
|
}
|
22
|
51
|
|
23
|
52
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
53
|
+ return crypt(text);
|
25
|
54
|
}
|
26
|
55
|
|
27
|
56
|
public static String rotate(String s, Character c) {
|
|
57
|
+ String[] rotator = s.split("");
|
|
58
|
+ StringBuilder result = new StringBuilder();
|
28
|
59
|
|
29
|
|
- return "";
|
|
60
|
+ for(int i = 0; i < rotator.length; i++) {
|
|
61
|
+ result.append(rotator[(s.indexOf(c) + i) % rotator.length]);
|
|
62
|
+ }
|
|
63
|
+ return result.toString();
|
30
|
64
|
}
|
31
|
65
|
|
32
|
66
|
}
|