|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+import static java.lang.Character.isLowerCase;
|
|
|
2
|
+import static java.lang.Character.isUpperCase;
|
|
|
3
|
+import static java.lang.Character.toLowerCase;
|
|
|
4
|
+
|
|
|
5
|
+public class ROT13 { //rotate13characters
|
|
|
6
|
+ //cryptography language: start w/ plaintext (data that has not yet been encryted) -> cryptmethodfunction ->
|
|
|
7
|
+ //produces ciphertext;
|
|
|
8
|
+// private final String uppercaseStart = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
9
|
+// private final String lowercaseStart = "abcdefghijklmnopqrstuvwxyz";
|
|
|
10
|
+ protected String startUpper;
|
|
|
11
|
+ protected String startLower;
|
|
|
12
|
+
|
|
|
13
|
+
|
|
|
14
|
+
|
|
|
15
|
+ ROT13(Character cs, Character cf) {
|
|
|
16
|
+ }
|
|
|
17
|
+
|
|
|
18
|
+ ROT13() {
|
|
|
19
|
+ }
|
|
|
20
|
+
|
|
|
21
|
+
|
|
|
22
|
+ public String crypt(String text) throws UnsupportedOperationException {
|
|
|
23
|
+ StringBuilder sb = new StringBuilder();
|
|
|
24
|
+ for (int i = 0; i < text.length(); i++) {
|
|
|
25
|
+ char ch = text.charAt(i);
|
|
|
26
|
+ if (ch > 'a' && ch <= 'm') {
|
|
|
27
|
+ ch += 13;
|
|
|
28
|
+ } else if (ch >= 'A' && ch <= 'M') {
|
|
|
29
|
+ ch += 13;
|
|
|
30
|
+ } else if (ch >= 'n' && ch <= 'z') {
|
|
|
31
|
+ ch -= 13;
|
|
|
32
|
+ } else if (ch >= 'N' && ch <= 'Z') {
|
|
|
33
|
+ ch -= 13;
|
|
|
34
|
+ }
|
|
|
35
|
+ sb.append(ch);
|
|
|
36
|
+ }
|
|
|
37
|
+ return sb.toString();
|
|
|
38
|
+ }
|
|
|
39
|
+
|
|
|
40
|
+ public String encrypt(String text) {
|
|
|
41
|
+ return crypt(text);
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ public String decrypt(String text) {
|
|
|
45
|
+ return crypt(text);
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+// StringBuilder sb = new StringBuilder();
|
|
|
49
|
+// for (int i = 0; i < text.length; i++) {
|
|
|
50
|
+// Character ch = text.charAt(i);
|
|
|
51
|
+// Integer position = 0;
|
|
|
52
|
+// if (isUpperCase(ch)) {
|
|
|
53
|
+// position = startUpper.indexOf(ch);
|
|
|
54
|
+// sb.append(registUpper.charAt(position));
|
|
|
55
|
+// } else if (isLowerCase(ch)) {
|
|
|
56
|
+// position = registerLower.indexOf(ch);
|
|
|
57
|
+// sb.append(startLower.charAt(position));
|
|
|
58
|
+// } else {
|
|
|
59
|
+// sb.append(ch);
|
|
|
60
|
+// }
|
|
|
61
|
+//
|
|
|
62
|
+// }
|
|
|
63
|
+//
|
|
|
64
|
+// return sb.toString();
|
|
|
65
|
+// }
|
|
|
66
|
+
|
|
|
67
|
+
|
|
|
68
|
+ public static String rotate(String s, Character c) {
|
|
|
69
|
+ int offset = s.indexOf(c);
|
|
|
70
|
+ int i = offset % s.length();
|
|
|
71
|
+ return s.substring(i) + s.substring(0, i);
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+}
|