到目前为止,这是我的代码。该程序旨在通过将每个字母移动字母表中的一定数量的字母来接收消息并加密或解密消息。我设法让这些程序相互通信(顺便提一下这里有两种方法),第一次提示工作,但是我不知道如何使解密工作以及大写或小写?非常感谢任何帮助。
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();
}
}
答案 0 :(得分:2)
您尝试通过传递-key
作为密钥来反转加密:
decryptedMessage = encrypt(message, -key);
然而,这条线因此而出错。
int index =(alphaLower.indexOf(message) + key)%26;
这是因为在Java中,%
运算符的负操作数将导致负值。这可以通过足够低的字符值和足够高的负键来实现。
您可以通过传递26 - key
作为解密“密钥”来解决此问题。
decryptedMessage = encrypt(message, 26 - key);
这将确保%
运算符的操作数保持为正。