|
@@ -6,11 +6,20 @@ import static java.lang.Character.toLowerCase;
|
6
|
6
|
|
7
|
7
|
public class ROT13 {
|
8
|
8
|
|
|
9
|
+ private String myCipher;
|
|
10
|
+ private String myAlphabet;
|
|
11
|
+
|
9
|
12
|
public static void main(String[] args) {
|
|
13
|
+
|
10
|
14
|
System.out.println("0384");
|
11
|
15
|
}
|
12
|
16
|
|
13
|
17
|
ROT13(Character cs, Character cf) {
|
|
18
|
+ myAlphabet = "abcdefghijklmnopqrstuvwxyz";
|
|
19
|
+ myCipher = rotate(myAlphabet,cf);
|
|
20
|
+ myAlphabet += myAlphabet.toUpperCase();
|
|
21
|
+ myCipher += myCipher.toUpperCase();
|
|
22
|
+
|
14
|
23
|
}
|
15
|
24
|
|
16
|
25
|
ROT13() {
|
|
@@ -19,15 +28,44 @@ public class ROT13 {
|
19
|
28
|
|
20
|
29
|
public String crypt(String text) throws UnsupportedOperationException {
|
21
|
30
|
|
22
|
|
- return "";
|
|
31
|
+ return encrypt(decrypt(text));
|
23
|
32
|
}
|
24
|
33
|
|
25
|
34
|
public String encrypt(String text) {
|
26
|
|
- 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;
|
27
|
50
|
}
|
28
|
51
|
|
29
|
52
|
public String decrypt(String text) {
|
30
|
|
- 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;
|
31
|
69
|
}
|
32
|
70
|
|
33
|
71
|
public static String rotate(String s, Character c) {
|