|
|
@@ -1,15 +1,18 @@
|
|
1
|
|
-
|
|
2
|
1
|
import java.util.Arrays;
|
|
3
|
2
|
import java.util.List;
|
|
4
|
3
|
|
|
5
|
|
-import static java.lang.Character.isLowerCase;
|
|
6
|
|
-import static java.lang.Character.isUpperCase;
|
|
7
|
|
-import static java.lang.Character.toLowerCase;
|
|
8
|
|
-
|
|
9
|
4
|
public class ROT13 {
|
|
10
|
5
|
private int rotate;
|
|
11
|
6
|
private int encryptShift = 26;
|
|
|
7
|
+ private boolean encrypt;
|
|
|
8
|
+
|
|
|
9
|
+ public boolean isEncrypt() {
|
|
|
10
|
+ return encrypt;
|
|
|
11
|
+ }
|
|
12
|
12
|
|
|
|
13
|
+ public void setEncrypt(boolean encrypt) {
|
|
|
14
|
+ this.encrypt = encrypt;
|
|
|
15
|
+ }
|
|
13
|
16
|
|
|
14
|
17
|
ROT13(Character cs, Character cf) {
|
|
15
|
18
|
rotate = (int)(cs) - (int)(cf);
|
|
|
@@ -26,54 +29,46 @@ public class ROT13 {
|
|
26
|
29
|
return encrypt(text);
|
|
27
|
30
|
}
|
|
28
|
31
|
|
|
29
|
|
-
|
|
30
|
|
- public String crypt(String text, int shift, int rotate, boolean encrypt) throws UnsupportedOperationException {
|
|
|
32
|
+ public String crypt(String text, int rotate) throws UnsupportedOperationException {
|
|
31
|
33
|
List<String> textList = Arrays.asList(text.split(" "));
|
|
32
|
34
|
StringBuilder builder = new StringBuilder();
|
|
33
|
35
|
|
|
34
|
36
|
for (String s: textList) {
|
|
35
|
37
|
char[] charArr = s.toCharArray();
|
|
36
|
38
|
for (char c: charArr) {
|
|
37
|
|
- if (encrypt)
|
|
38
|
|
- builder.append(shiftCharEncrypt(c, shift, rotate));
|
|
39
|
|
- else {
|
|
40
|
|
- builder.append(shiftCharDecrypt(c, shift, rotate));
|
|
41
|
|
- }
|
|
|
39
|
+ String strToAppend = isEncrypt() ? shiftCharEncrypt(c, rotate) : shiftCharDecrypt(c, rotate);
|
|
|
40
|
+ builder.append(strToAppend);
|
|
42
|
41
|
}
|
|
43
|
42
|
builder.append(" ");
|
|
44
|
43
|
}
|
|
45
|
|
-
|
|
46
|
44
|
return builder.toString().trim();
|
|
47
|
45
|
}
|
|
48
|
46
|
|
|
49
|
47
|
public String encrypt(String text) {
|
|
50
|
|
- return crypt(text, encryptShift, getRotate(), true);
|
|
|
48
|
+ setEncrypt(true);
|
|
|
49
|
+ return crypt(text, getRotate());
|
|
51
|
50
|
}
|
|
52
|
51
|
|
|
53
|
52
|
public String decrypt(String text) {
|
|
54
|
|
-
|
|
55
|
|
- return crypt(text, encryptShift * -1, getRotate() * -1, false);
|
|
|
53
|
+ setEncrypt(false);
|
|
|
54
|
+ return crypt(text, getRotate() * -1);
|
|
56
|
55
|
}
|
|
57
|
56
|
|
|
58
|
|
- public String shiftCharEncrypt(char c, int shift, int rotate){
|
|
|
57
|
+ public String shiftCharEncrypt(char c, int rotate){
|
|
59
|
58
|
if (c > 64){
|
|
60
|
|
- if ( (c > 96 && (c+ rotate) > 96) || (c <= 96 && (c+ rotate) > 64))
|
|
61
|
|
- return ( Character.toString( (char)(c+ rotate) ) );
|
|
62
|
|
- else {
|
|
63
|
|
- return ( Character.toString( (char)(c+ rotate +shift) ) );
|
|
64
|
|
- }
|
|
|
59
|
+ return ( (c > 96 && (c + rotate) > 96) || (c <= 96 && (c + rotate) > 64) ) ?
|
|
|
60
|
+ ( Character.toString( (char)(c + rotate) ) ) :
|
|
|
61
|
+ ( Character.toString( (char)(c + rotate + encryptShift) ) );
|
|
65
|
62
|
} else {
|
|
66
|
63
|
return Character.toString(c);
|
|
67
|
64
|
}
|
|
68
|
65
|
}
|
|
69
|
66
|
|
|
70
|
|
- public String shiftCharDecrypt(char c, int shift, int rotate){
|
|
|
67
|
+ public String shiftCharDecrypt(char c, int rotate){
|
|
71
|
68
|
if (c > 64){
|
|
72
|
|
- if ( (c + rotate > 122) || (c <= 90 && c + rotate > 90 ) ) // ( (c < 123 && (c+ rotate) < 123) || (c <= 123 && (c+ rotate) > 64))
|
|
73
|
|
- return ( Character.toString( (char)(c+ rotate +shift) ) );
|
|
74
|
|
- else {
|
|
75
|
|
- return ( Character.toString( (char)(c+ rotate) ) );
|
|
76
|
|
- }
|
|
|
69
|
+ return ( (c + rotate > 122) || (c <= 90 && c + rotate > 90 ) ) ?
|
|
|
70
|
+ ( Character.toString( (char)(c + rotate + encryptShift * -1) ) ) :
|
|
|
71
|
+ ( Character.toString( (char)(c + rotate) ) );
|
|
77
|
72
|
} else {
|
|
78
|
73
|
return Character.toString(c);
|
|
79
|
74
|
}
|
|
|
@@ -94,22 +89,7 @@ public class ROT13 {
|
|
94
|
89
|
returnChar[j] = (char)( (int)(charArr[i]));
|
|
95
|
90
|
j++;
|
|
96
|
91
|
}
|
|
97
|
|
-
|
|
98
|
92
|
return String.copyValueOf(returnChar);
|
|
99
|
93
|
}
|
|
100
|
94
|
|
|
101
|
95
|
}
|
|
102
|
|
-// public String encrypt(String word) {
|
|
103
|
|
-// char[] charArr = word.toCharArray();
|
|
104
|
|
-// for(int i = 0; i < charArr.length; i++)
|
|
105
|
|
-// {
|
|
106
|
|
-// int tempInt = ((int)charArr[i]) + 3;
|
|
107
|
|
-// if(tempInt > 122)
|
|
108
|
|
-// {
|
|
109
|
|
-// tempInt -= 26;
|
|
110
|
|
-// }
|
|
111
|
|
-// charArr[i] = (char)tempInt;
|
|
112
|
|
-// }
|
|
113
|
|
-// String returnStr = new String(charArr);
|
|
114
|
|
-// return returnStr;
|
|
115
|
|
-// }
|