-
Notifications
You must be signed in to change notification settings - Fork 33
/
Caesar.java
40 lines (31 loc) · 886 Bytes
/
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
package cryptography.ciphers.caesar;
import cryptography.Mode;
public class Caesar {
public static void main(String[] args) {
}
/**
* Caesar cipher
*
* @param inputText Text to cipher / decipher
* @param shiftCount Alphabet shift count
* @param mode encrypt or decrypt mode
* @return String based on selected mode
*/
public static String caesar(String inputText, int shiftCount, final Mode mode) {
try {
String output = "";
int ascii;
if (mode == Mode.DECRYPT) {
shiftCount=-1*shiftCount;
}
shiftCount=(shiftCount+26)%26;
for(int a=0;a<inputText.length();a++){
ascii=(((int)inputText.charAt(a))+shiftCount);
output+=(ascii>'Z')?Character.toString((char)((ascii%'Z')+'A'-1)):Character.toString((char)(ascii));
}
return output;
} catch (ArrayIndexOutOfBoundsException e) {
return e.toString();
}
}
}