I am working on a caesar cipher program as my final project in java I. I have a method for the encryption that handles lower and uppercase characters. However I have hit a wall trying to adjust the algorithm to work the shift factor in the opposite direction to decrypt text already run through the program.
Another issue is that I cannot make the program work with spaces, either ignoring them or encrypting them as well. So far it only encrypts the first word and ignores the rest. Here is my encryption method thus far, with the message passed from main() as a parameter.
//Caesar Cipher
public class EncryptionClass1 {
protected static String encryptedMessageString;
public static String setEncryptedMessage(String message) {
StringBuffer encryptedMessageBuffer = new StringBuffer();
char newCharacter;
int shiftFactor = 1;
int i;
//for length of secret message
for (i = 0; i<message.length(); i++) {
//uppercase encoder
if (Character.isUpperCase(message.charAt(i))) {
newCharacter = (char)(((int)message.charAt(i) -
shiftFactor - 65) % 26 + 65);
encryptedMessageBuffer.append(newCharacter);
}//if close
//lowercase encoder
else {
newCharacter = (char)(((int)message.charAt(i) -
shiftFactor - 97) % 26 + 97);
encryptedMessageBuffer.append(newCharacter);
}//else close
}//for close
//convert StringBuffer to string
encryptedMessageString = encryptedMessageBuffer.toString();
return encryptedMessageString;
}//setEncryptedMessage close
}//EncryptionClass1 close
For the decryption, I figure that I only need to adjust some of the operations for the encryption method, but I cant pinpoint what.
as for the spaces, an input and output example should be as follows:
Input: “my secret message”
Output: rp dbxbbf rbddtfb