2
2
3
3
public class CaesarShift {
4
4
private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ; //Encapsulate and make the alphabet immutable
5
- private static int key = 25 ; //Initialize and encapsulate the shift key
5
+ private static int key = 25 ; //Initialize and encapsulate the shift key
6
6
private static String encryptedMsg ; //Create and encapsulate the message container
7
- private static String decryptedMsg ;
7
+ private static String decryptedMsg ; //Create and encapsulate the decrypted message container
8
8
9
- public String encrypt (String encryptMsg ){
9
+ public String encrypt (String encryptMsg ) {
10
10
String upCased = encryptMsg .toUpperCase ();
11
11
char [] upCasedArrs = upCased .toCharArray ();//split the string to a character array
12
12
ArrayList <Character > encryptedChars = new ArrayList <Character >();
13
13
14
14
//loop through the character array
15
- for (Character character : upCasedArrs ) {
15
+ for (Character character : upCasedArrs ) {
16
16
int index = ALPHABET .indexOf (character .toString ());//get the character rank in the alphabet
17
- int encryptedCharIndex = Math .floorMod ((index + key ),26 );//shift the character using the key and get the new characters rank in the alphabet
17
+ int encryptedCharIndex = Math .floorMod ((index + key ), 26 );//shift the character using the key and get the new characters rank in the alphabet
18
18
encryptedChars .add (ALPHABET .charAt (encryptedCharIndex ));//get the character from the alphabet rank and add it to the char array
19
- encryptedMsg = encryptedChars .toString ().replaceAll ("\\ [|\\ ]|\\ s" ,"" ).replaceAll ("," ,"" );//convert and cleanup the char array to a string
19
+ encryptedMsg = encryptedChars .toString ().replaceAll ("\\ [|\\ ]|\\ s" , "" ).replaceAll ("," , "" );//convert and cleanup the char array to a string
20
20
}
21
21
return encryptedMsg ;
22
22
}
@@ -37,4 +37,20 @@ public String decrypt(String decryptMsg) {
37
37
38
38
}
39
39
40
+ public static int getKey () {
41
+ return key ;
42
+ }
43
+
44
+ public static void setKey (int key ) {
45
+ CaesarShift .key = key ;
46
+ }
47
+
48
+ public static String getEncryptedMsg () {
49
+ return encryptedMsg ;
50
+ }
51
+
52
+ public static String getDecryptedMsg () {
53
+ return decryptedMsg ;
54
+ }
55
+
40
56
}
0 commit comments