diff --git a/Cipher.java b/Cipher.java index 7527c5b..f63ca39 100644 --- a/Cipher.java +++ b/Cipher.java @@ -1,6 +1,5 @@ // This class is used for encrypting or decrypting strings using character mapping -public class Cipher -{ +public class Cipher { // Strings for keeping the alphabets, one for the original letters and the other for the encrypted ones // encryption involves mapping from original to cipher, for each letter we locate the character in the // original string and replace it with the cipher alphabet letter at the same position @@ -8,54 +7,49 @@ public class Cipher public static final String CIPHER_ALPHABET = "dfxyhrklvwuasgimnojpqetbcz"; public String encrypt(String inputString) { - - // output string will be collected in this variable, one char at a time + // Output string will be collected in this variable, one char at a time String outputString = ""; - - // for all chars in the input string - for (int i = 0; i < inputString.length(); i++) - { + // For all chars in the input string + for (int i = 0; i < inputString.length(); i++) { + // Append the encrypted version of the char to the output string + outputString += replaceChar(inputString.charAt(i), true); } return outputString; } public String decrypt(String inputString) { - - // output string will be collected in this variable, one char at a time + // Output string will be collected in this variable, one char at a time String outputString = ""; - - replaceChar('a',true); - + + // For all chars in the input string + for (int i = 0; i < inputString.length(); i++) { + // Append the decrypted version of the char to the output string + outputString += replaceChar(inputString.charAt(i), false); + } + return outputString; } - // replaces the given input char based on the given isEncrypt variable + // Replaces the given input char based on the given isEncrypt variable // if isEncrypt == true -> original to encrypted // if isEncrypt == false -> encrypted to original - // works only when the input char is included in our alphabet variables - // should not replace symbols or upper case letters, return input char in those cases + // Works only when the input char is included in our alphabet variables + // Should not replace symbols or upper-case letters, return input char in those cases private char replaceChar(char inputChar, boolean isEncrypt) { - - if(isEncrypt) { - for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) - { - if(ORIGINAL_ALPHABET.charAt(i) == inputChar) { - - } + if (isEncrypt) { + int index = ORIGINAL_ALPHABET.indexOf(inputChar); + if (index != -1) { + return CIPHER_ALPHABET.charAt(index); } - } - else { - for (int i = 0; i < CIPHER_ALPHABET.length(); i++) - { - if(CIPHER_ALPHABET.charAt(i) == inputChar) { - return ORIGINAL_ALPHABET.charAt(i); - } + } else { + int index = CIPHER_ALPHABET.indexOf(inputChar); + if (index != -1) { + return ORIGINAL_ALPHABET.charAt(index); } } - - // if we did not find it in the alphabet, then return the original char + // If the character is not found in our alphabets, return it as is return inputChar; } -} \ No newline at end of file +}