|
|
@@ -1,5 +1,4 @@
|
|
1
|
1
|
|
|
2
|
|
-import java.util.ArrayList;
|
|
3
|
2
|
import java.util.Arrays;
|
|
4
|
3
|
import java.util.List;
|
|
5
|
4
|
|
|
|
@@ -8,25 +7,33 @@ import static java.lang.Character.isUpperCase;
|
|
8
|
7
|
import static java.lang.Character.toLowerCase;
|
|
9
|
8
|
|
|
10
|
9
|
public class ROT13A {
|
|
11
|
|
- private int rotateBy;
|
|
|
10
|
+ private int rotate;
|
|
12
|
11
|
private int encryptShift = 26;
|
|
13
|
|
- private int decryptShift = -26;
|
|
|
12
|
+
|
|
14
|
13
|
|
|
15
|
14
|
ROT13A(Character cs, Character cf) {
|
|
16
|
|
- rotateBy = (int)(cs) - (int)(cf);
|
|
|
15
|
+ rotate = (int)(cs) - (int)(cf);
|
|
17
|
16
|
}
|
|
18
|
17
|
|
|
19
|
18
|
ROT13A() {
|
|
20
|
19
|
}
|
|
21
|
20
|
|
|
22
|
|
- public String crypt(String text, int shift) throws UnsupportedOperationException {
|
|
|
21
|
+ public int getRotate() {
|
|
|
22
|
+ return rotate;
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ public String crypt(String text, int shift, int rotate, boolean encrypt) throws UnsupportedOperationException {
|
|
23
|
26
|
List<String> textList = Arrays.asList(text.split(" "));
|
|
24
|
27
|
StringBuilder builder = new StringBuilder();
|
|
25
|
28
|
|
|
26
|
29
|
for (String s: textList) {
|
|
27
|
30
|
char[] charArr = s.toCharArray();
|
|
28
|
31
|
for (char c: charArr) {
|
|
29
|
|
- builder.append(shiftChar(c, shift));
|
|
|
32
|
+ if (encrypt)
|
|
|
33
|
+ builder.append(shiftCharEncrypt(c, shift, rotate));
|
|
|
34
|
+ else {
|
|
|
35
|
+ builder.append(shiftCharDecrypt(c, shift, rotate));
|
|
|
36
|
+ }
|
|
30
|
37
|
}
|
|
31
|
38
|
builder.append(" ");
|
|
32
|
39
|
}
|
|
|
@@ -35,24 +42,36 @@ public class ROT13A {
|
|
35
|
42
|
}
|
|
36
|
43
|
|
|
37
|
44
|
public String encrypt(String text) {
|
|
38
|
|
- return crypt(text, encryptShift);
|
|
|
45
|
+ return crypt(text, encryptShift, getRotate(), true);
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ public String decrypt(String text) {
|
|
|
49
|
+
|
|
|
50
|
+ return crypt(text, encryptShift * -1, getRotate() * -1, false);
|
|
39
|
51
|
}
|
|
40
|
52
|
|
|
41
|
|
- public String shiftChar(char c, int shift){
|
|
|
53
|
+ public String shiftCharEncrypt(char c, int shift, int rotate){
|
|
42
|
54
|
if (c > 64){
|
|
43
|
|
- if ( (c > 96 && (c+rotateBy) > 96) || (c < 97 && (c+rotateBy) > 64))
|
|
44
|
|
- return ( Character.toString( (char)(c+rotateBy) ) );
|
|
|
55
|
+ if ( (c > 96 && (c+ rotate) > 96) || (c <= 96 && (c+ rotate) > 64))
|
|
|
56
|
+ return ( Character.toString( (char)(c+ rotate) ) );
|
|
45
|
57
|
else {
|
|
46
|
|
- return ( Character.toString( (char)(c+rotateBy+shift) ) );
|
|
|
58
|
+ return ( Character.toString( (char)(c+ rotate +shift) ) );
|
|
47
|
59
|
}
|
|
48
|
60
|
} else {
|
|
49
|
61
|
return Character.toString(c);
|
|
50
|
62
|
}
|
|
51
|
63
|
}
|
|
52
|
64
|
|
|
53
|
|
- public String decrypt(String text) {
|
|
54
|
|
-
|
|
55
|
|
- return crypt(text, decryptShift);
|
|
|
65
|
+ public String shiftCharDecrypt(char c, int shift, int rotate){
|
|
|
66
|
+ if (c > 64){
|
|
|
67
|
+ if ( (c + rotate > 122) || (c <= 90 && c + rotate > 90 ) ) // ( (c < 123 && (c+ rotate) < 123) || (c <= 123 && (c+ rotate) > 64))
|
|
|
68
|
+ return ( Character.toString( (char)(c+ rotate +shift) ) );
|
|
|
69
|
+ else {
|
|
|
70
|
+ return ( Character.toString( (char)(c+ rotate) ) );
|
|
|
71
|
+ }
|
|
|
72
|
+ } else {
|
|
|
73
|
+ return Character.toString(c);
|
|
|
74
|
+ }
|
|
56
|
75
|
}
|
|
57
|
76
|
|
|
58
|
77
|
public static String rotate(String s, Character c) {
|