-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar.java
189 lines (155 loc) · 5.84 KB
/
Caesar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.*;
import java.util.*;
/**
* @author: cs11wml
* @pid: A15017352
* @date: 1/16/2018
* @about: Encrypt and decrypt string using different type method
* */
public class Caesar {
/** Check whether rotation is in the range 0-26, if not modify it, else return
* the original one.
* @param rotation The number of time the letter wheel rotates
* @return The (modified) rotation in range 0-26
* */
private static int rotationSetter(int rotation) {
// Check rotation of negative and large num Case
// Large positive
if (rotation > 26)
rotation = rotation % 26;
// Small negative
if (rotation < 0 && rotation >= -26)
rotation = 26 + rotation;
// Large negative
if (rotation < -26)
rotation = 26 - (Math.abs(rotation) % 26);
return rotation;
}
/** Return the char after rotation, if char is not alphabetical return the original
* @param a The character being rotated
* @param rotation The number of time the letter wheel rotates
* @return The alphabetical character after rotation, or the original
* */
private static char letterOperation(char a, int rotation) {
// Make sure the rotaion is in range
rotation = rotationSetter(rotation);
if (a >= 65 && a <= 90){
// Check if a is in the range of Upper Case after rotation
if (a <= 90 - rotation)
a += rotation;
else
a = (char)(a + rotation - 26); // if not start a by -26
}
// Check if char is in Lower Case range
else if (a >= 97 && a <= 122){
if (a <= 122 - rotation)
a += rotation;
else
a = (char)(a + rotation - 26); // if not start a by +26
}
return a;
}
/** Return the string after encryption using String type object
* @param s The string being encrypted
* @param rotation The number of time the letter wheel rotates
* @return The encrypted string
* */
public static String encryptStr(String s, int rotation) {
// initialize the return string (String type)
String result = "";
// traverse the string and rotate each character by using letterOperation
// then add it to the result string
for (int i = 0; i < s.length(); i++){
char a = s.charAt(i);
result += letterOperation(a, rotation);
}
return result;
}
/** Return the string after decryption using String type object
* @param s The string being decrypted
* @param rotation The number of time the letter wheel rotates
* @return The decrypted string
* */
public static String decryptStr(String s, int rotation) {
// initialize the return string (String type)
String result = "";
// traverse the string and rotate each character by using letterOperation
// then added it to the result string
for (int i = 0; i < s.length(); i++){
char a = s.charAt(i);
// decryption rotates different direction, which is 26 - rotation
result += letterOperation(a, 26 - rotation);
}
return result;
}
/** Return the string after encryption using char array type object
* @param s The string being encrypted
* @param rotation The number of time the letter wheel rotates
* @return The encrypted string
* */
public static String encryptChArr(String s, int rotation) {
// convert string to char array
char[] array_s = s.toCharArray();
// traverse the array and modify each character by using letterOperation
for (int i = 0; i < array_s.length; i++){
char a = array_s[i];
array_s[i] = letterOperation(a, rotation);
}
// convert char array to String and return it
String result = new String(array_s);
return result;
}
/** Return the string after decryption using char array type object
* @param s The string being decrypted
* @param rotation The number of time the letter wheel rotates
* @return The decrypted string
* */
public static String decryptChArr(String s, int rotation) {
// convert string to char array
char[] array_s = s.toCharArray();
// traverse the array and modify each character by using letterOperation
for (int i = 0; i < array_s.length; i++){
char a = array_s[i];
// decryption rotates different direction, which is 26 - rotation
array_s[i] = letterOperation(a, 26 - rotation);
}
// convert char array to String and return it
String result = new String(array_s);
return result;
}
/** Return the string after encryption using StringBuilder type object
* @param s The string being encrypted
* @param rotation The number of time the letter wheel rotates
* @return The encrypted string
* */
public static String encryptSB(String s, int rotation) {
// initialize result as StringBuilder type
StringBuilder result = new StringBuilder();
// traverse the string and rotate each character by using letterOperation
// then append it to the result StringBuilder
for (int i = 0; i < s.length(); i++){
char a = s.charAt(i);
result.append(letterOperation(a, rotation));
}
// convert the result to string and return it
return result.toString();
}
/** Return the string after decryption using StringBuilder type object
* @param s The string being decrypted
* @param rotation The number of time the letter wheel rotates
* @return The decrypted string
* */
public static String decryptSB(String s, int rotation) {
// initialize result as StringBuilder type
StringBuilder result = new StringBuilder();
// traverse the string and rotate each character by using letterOperation
// then append it to the result StringBuilder
for (int i = 0; i < s.length(); i++){
char a = s.charAt(i);
// decryption rotates different direction, which is 26 - rotation
result.append(letterOperation(a, 26 - rotation));
}
// convert the result to string and return it
return result.toString();
}
}