Reputation: 21
Here is my code so far. The program is designed to take a message and encrypt or decrypt a message by shifting each letter a certain number of letters in the alphabet. I managed to get the programs talking to each other (there are two methods here by the way) and the first prompt works, however I am not sure how to get the decryption working as well as with either upper or lower case? Any help is greatly appreciated.
public class CaesarCipher
{
public static String encrypt(String message, int key)
{
String alphaLower = "abcdefghijklmnopqrstuvwxyz";
String alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int sum = 0;
String encryptedMessage = "";
//Find the index of every char in the string "message"
//Add 1 to every index.
//Take that index and turn it back into a char.
for (int i = 0; i < message.length(); i++)
{
if (alphaLower.indexOf(message.charAt(i)) > -1)
{char letter = message.charAt(i);
int index =(alphaLower.indexOf(message) + key)%26;
char newLetter = alphaLower.charAt(index);
encryptedMessage += newLetter;
}
else if (alphaUpper.indexOf(message.charAt(i)) )
{char letter = message.charAt(i);
int index = alphaUpper.indexOf(message) + key;
char newLetter = alphaUpper.charAt(index);
encryptedMessage += newLetter;
}
else
{
encryptedMessage
}
}
return encryptedMessage;
}
public static String decrypt(String message, int key)
{
String decryptedMessage;
decryptedMessage = encrypt(message, -key)
return decryptedMessage;
}
}
public class CaesarTester
{
public static void main(String[] args)
{
System.out.println("Test 1 - simple encrypt:");
System.out.println("Expected: j offe npofz");
System.out.println("Actual: " + CaesarCipher.encrypt("i need money", 1));
System.out.println();
System.out.println("Test 2 - simple decrypt:");
System.out.println("Expected: i need money");
System.out.println("Actual: " + CaesarCipher.decrypt("j offe npofz", 1));
System.out.println();
System.out.println("Test 3 - simple encrypt w/ casing:");
System.out.println("Expected: Fyx M jsvksx qc TMR jsv xli EXQ");
System.out.println("Actual: " + CaesarCipher.encrypt("But I forgot my PIN for the ATM", 4));
System.out.println();
System.out.println("Test 4 - simple decrypt w/ casing:");
System.out.println("Expected: But I forgot my PIN for the ATM");
System.out.println("Actual: " + CaesarCipher.decrypt("Fyx M jsvksx qc TMR jsv xli EXQ", 4));
System.out.println();
System.out.println("Test 5 - simple encrypt w/ casing and symbols:");
System.out.println("Expected: Un xomnz. VOT oy 1234!");
System.out.println("Actual: " + CaesarCipher.encrypt("Oh right. PIN is 1234!", 6));
System.out.println();
System.out.println("Test 6 - simple decrypt w/ casing and symbols:");
System.out.println("Expected: Oh right. PIN is 1234!");
System.out.println("Actual: " + CaesarCipher.decrypt("Un xomnz. VOT oy 1234!", 6));
System.out.println();
System.out.println("Test 7 - complex encrypt:");
System.out.println("Expected: Hiq, qbun'm gs qczc jummqilx?!");
System.out.println("Actual: " + CaesarCipher.encrypt("Now, what's my wifi password?!", 20));
System.out.println();
System.out.println("Test 8 - complex decrypt:");
System.out.println("Expected: Now, what's my wifi password?!");
System.out.println("Actual: " + CaesarCipher.decrypt("Hiq, qbun'm gs qczc jummqilx?!", 20));
System.out.println();
}
}
Upvotes: 1
Views: 324
Reputation: 178333
You are attempting to reverse the encryption by passing in -key
as the key here:
decryptedMessage = encrypt(message, -key);
However, this line will go wrong because of that.
int index =(alphaLower.indexOf(message) + key)%26;
This is because in Java, a negative operand to the %
operator will result in a negative value. This can occur with a low enough character value and a negative key of high enough magnitude.
You can rectify this by passing in 26 - key
as your decryption "key".
decryptedMessage = encrypt(message, 26 - key);
This will ensure that the operand to the %
operator remains positive.
Upvotes: 2