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