diff --git a/src/main/java/CaesarShift.java b/src/main/java/CaesarShift.java new file mode 100644 index 0000000..e603113 --- /dev/null +++ b/src/main/java/CaesarShift.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; + +public class CaesarShift { + private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Encapsulate and make the alphabet immutable + private static int key = 25; //Initialize and encapsulate the shift key + private static String encryptedMsg; //Create and encapsulate the message container + private static String decryptedMsg; + + public String encrypt(String encryptMsg){ + String upCased = encryptMsg.toUpperCase(); + char[] upCasedArrs = upCased.toCharArray();//split the string to a character array + ArrayList encryptedChars = new ArrayList(); + + //loop through the character array + for(Character character : upCasedArrs ){ + int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet + int encryptedCharIndex = Math.floorMod((index+key),26);//shift the character using the key and get the new characters rank in the alphabet + encryptedChars.add(ALPHABET.charAt(encryptedCharIndex));//get the character from the alphabet rank and add it to the char array + encryptedMsg = encryptedChars.toString().replaceAll("\\[|\\]|\\s","").replaceAll(",","");//convert and cleanup the char array to a string + } + return encryptedMsg; + } + + public String decrypt(String decryptMsg) { + String upCased = decryptMsg.toUpperCase(); + char[] upCasedArrs = upCased.toCharArray();//split the string to a character array + ArrayList decryptedChars = new ArrayList(); + + //loop through the character array + for (Character character : upCasedArrs) { + int index = ALPHABET.indexOf(character.toString());//get the character rank in the alphabet + int decryptedCharIndex = Math.floorMod((index - key), 26);//shift the character using the key and get the new characters rank in the alphabet + decryptedChars.add(ALPHABET.charAt(decryptedCharIndex));//get the character from the alphabet rank and add it to the char array + decryptedMsg = decryptedChars.toString().replaceAll("\\[|\\]|\\s", "").replaceAll(",", "");//convert and cleanup the char array to a string + } + return decryptedMsg; + + } + +} diff --git a/src/test/java/CaesarShiftTest.java b/src/test/java/CaesarShiftTest.java new file mode 100644 index 0000000..856e795 --- /dev/null +++ b/src/test/java/CaesarShiftTest.java @@ -0,0 +1,31 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CaesarShiftTest { + @Test + public void caesarShift_isEncryptUpcasedTest_Boolean() { + CaesarShift encryptCaseCheck = new CaesarShift(); + assertEquals(true, Character.isUpperCase(encryptCaseCheck.encrypt("a").charAt(0))); + } + + @Test + void caesarShift_encryptStringTest_String() { + CaesarShift encryptString = new CaesarShift(); + assertEquals("GDKKN", encryptString.encrypt("hello")); + } + + @Test + public void caesarShift_isDecryptMsgUpcasedTest_String() { + CaesarShift caseTest = new CaesarShift(); + assertEquals(true, Character.isUpperCase(caseTest.decrypt("a").charAt(0))); + } + + @Test + public void caesarShift_decryptStringTest_String(){ + CaesarShift decryptStringTest = new CaesarShift(); + assertEquals("Z", decryptStringTest.decrypt("Y")); + } + + +} \ No newline at end of file