|
@@ -0,0 +1,91 @@
|
|
1
|
+import java.util.Arrays;
|
|
2
|
+import java.util.List;
|
|
3
|
+import java.util.Map;
|
|
4
|
+import java.util.stream.Stream;
|
|
5
|
+
|
|
6
|
+import static java.lang.Character.isLowerCase;
|
|
7
|
+import static java.lang.Character.isUpperCase;
|
|
8
|
+import static java.lang.Character.toLowerCase;
|
|
9
|
+
|
|
10
|
+public class ROT13 {
|
|
11
|
+
|
|
12
|
+ Character original;
|
|
13
|
+ Character adjusted;
|
|
14
|
+ int shift = 13;
|
|
15
|
+ String alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
16
|
+ String shifted = "NOPQRSTUVWXYZABCDEFGHIJKLM";
|
|
17
|
+
|
|
18
|
+ ROT13(Character cs, Character cf) {
|
|
19
|
+ this.original = cs;
|
|
20
|
+ this.adjusted = cf;
|
|
21
|
+ this.shift = cf - cs;
|
|
22
|
+ this.shifted = rotate(alpha, cf);
|
|
23
|
+
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ ROT13() {
|
|
27
|
+
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ public String getShifted() {
|
|
31
|
+ return shifted;
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+ public String crypt(String text) throws UnsupportedOperationException {
|
|
36
|
+
|
|
37
|
+ return text;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ public String encrypt(String text) {
|
|
41
|
+ String result = "";
|
|
42
|
+ text = text.toUpperCase();
|
|
43
|
+ for (Character c: text.toCharArray()) {
|
|
44
|
+ 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);
|
|
48
|
+ }
|
|
49
|
+ }
|
|
50
|
+ } else {
|
|
51
|
+ result += c;
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ return result.charAt(0) + result.substring(1).toLowerCase();
|
|
57
|
+
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ public String decrypt(String text) {
|
|
61
|
+ String result = "";
|
|
62
|
+ text = text.toUpperCase();
|
|
63
|
+ for (Character c: text.toCharArray()) {
|
|
64
|
+ 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);
|
|
68
|
+ }
|
|
69
|
+ }
|
|
70
|
+ } else {
|
|
71
|
+ result += c;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ return result.charAt(0) + result.substring(1).toLowerCase();
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ public static String rotate(String s, Character c) {
|
|
80
|
+
|
|
81
|
+ c = Character.toUpperCase(c);
|
|
82
|
+ int index = 0;
|
|
83
|
+ for (int i = 0; i < s.length(); i++) {
|
|
84
|
+ if(s.charAt(i) == c){
|
|
85
|
+ index = i;
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+ return s.substring(index) + s.substring(0, index);
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+}
|