22
33public class CaesarShift {
44 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
66 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
88
9- public String encrypt (String encryptMsg ){
9+ public String encrypt (String encryptMsg ) {
1010 String upCased = encryptMsg .toUpperCase ();
1111 char [] upCasedArrs = upCased .toCharArray ();//split the string to a character array
1212 ArrayList <Character > encryptedChars = new ArrayList <Character >();
1313
1414 //loop through the character array
15- for (Character character : upCasedArrs ) {
15+ for (Character character : upCasedArrs ) {
1616 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
1818 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
2020 }
2121 return encryptedMsg ;
2222 }
@@ -37,4 +37,20 @@ public String decrypt(String decryptMsg) {
3737
3838 }
3939
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+
4056}
0 commit comments