Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions Cipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ 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
String outputString = "";

// 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
String outputString = "";


// 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), false);
}

replaceChar('a',true);

return outputString;
Expand All @@ -37,11 +46,12 @@ public String decrypt(String inputString) {
// 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) {
return CIPHER_ALPHABET.charAt(i);

}
}
Expand All @@ -54,8 +64,8 @@ private char replaceChar(char inputChar, boolean isEncrypt) {
}
}
}

// if we did not find it in the alphabet, then return the original char
return inputChar;
}
}
}