|
@@ -1,10 +1,25 @@
|
|
1
|
+package src;
|
|
2
|
+
|
1
|
3
|
import static java.lang.Character.isLowerCase;
|
2
|
4
|
import static java.lang.Character.isUpperCase;
|
3
|
5
|
import static java.lang.Character.toLowerCase;
|
4
|
6
|
|
5
|
7
|
public class ROT13 {
|
6
|
8
|
|
|
9
|
+ private String myCipher;
|
|
10
|
+ private String myAlphabet;
|
|
11
|
+
|
|
12
|
+ public static void main(String[] args) {
|
|
13
|
+
|
|
14
|
+ System.out.println("0384");
|
|
15
|
+ }
|
|
16
|
+
|
7
|
17
|
ROT13(Character cs, Character cf) {
|
|
18
|
+ myAlphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
19
|
+ myCipher = rotate(myAlphabet,cf);
|
|
20
|
+ myAlphabet += myAlphabet.toUpperCase();
|
|
21
|
+ myCipher += myCipher.toUpperCase();
|
|
22
|
+
|
8
|
23
|
}
|
9
|
24
|
|
10
|
25
|
ROT13() {
|
|
@@ -13,20 +28,63 @@ public class ROT13 {
|
13
|
28
|
|
14
|
29
|
public String crypt(String text) throws UnsupportedOperationException {
|
15
|
30
|
|
16
|
|
- return "";
|
|
31
|
+ return encrypt(decrypt(text));
|
17
|
32
|
}
|
18
|
33
|
|
19
|
34
|
public String encrypt(String text) {
|
20
|
|
- return text;
|
|
35
|
+
|
|
36
|
+ String encyrptedMessage = "";
|
|
37
|
+
|
|
38
|
+ for (int i = 0; i < text.length(); i++){
|
|
39
|
+ char charToRotate = text.charAt(i);
|
|
40
|
+ int indexOfCurrentStringLetter = myAlphabet.indexOf(charToRotate);
|
|
41
|
+ if (indexOfCurrentStringLetter == -1){
|
|
42
|
+ encyrptedMessage+=charToRotate;
|
|
43
|
+ } else {
|
|
44
|
+ encyrptedMessage += Character.toString(myCipher.charAt(indexOfCurrentStringLetter));
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ }
|
|
48
|
+
|
|
49
|
+ return encyrptedMessage;
|
21
|
50
|
}
|
22
|
51
|
|
23
|
52
|
public String decrypt(String text) {
|
24
|
|
- return text;
|
|
53
|
+
|
|
54
|
+ String decryptedMessage = "";
|
|
55
|
+
|
|
56
|
+ for (int i = 0; i < text.length(); i++){
|
|
57
|
+ char charToRotate = text.charAt(i);
|
|
58
|
+ int indexOfCurrentStringLetter = myCipher.indexOf(charToRotate);
|
|
59
|
+ if (indexOfCurrentStringLetter == -1){
|
|
60
|
+ decryptedMessage+=charToRotate;
|
|
61
|
+ } else {
|
|
62
|
+ decryptedMessage += Character.toString(myAlphabet.charAt(indexOfCurrentStringLetter));
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ return decryptedMessage;
|
25
|
69
|
}
|
26
|
70
|
|
27
|
71
|
public static String rotate(String s, Character c) {
|
|
72
|
+// int charIndex = 0;
|
28
|
73
|
|
29
|
|
- return "";
|
|
74
|
+// char[] charArray = s.toCharArray();
|
|
75
|
+// for(int x = 0; x < charArray.length; x++){
|
|
76
|
+// if (charArray[x] == c){
|
|
77
|
+// charIndex = x;
|
|
78
|
+// }
|
|
79
|
+// }
|
|
80
|
+ int charIndex = s.indexOf(c);
|
|
81
|
+ String rightHalf = s.substring(charIndex);
|
|
82
|
+ String leftHalf = s.substring(0,charIndex);
|
|
83
|
+
|
|
84
|
+ return rightHalf + leftHalf;
|
30
|
85
|
}
|
31
|
86
|
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
32
|
90
|
}
|