作业2:两把钥匙
编写一个具有一个名为input的String参数的decrypt方法。此方法返回一个String,它是使用与此CaesarCipherTwo对象关联的key1和key2解密的加密String。您可能希望向该类添加更多私有字段。
有人可以帮我吗?我对如何为解密方法编写代码没有任何想法。我认为它应该有一个return语句,我有两个。 “与CaesarCipherTwo对象相关联”在如何编写代码方面意味着什么?
这就是我已经拥有的。欢迎提出任何建议:
import edu.duke.*;
public class CaesarCipherTwoKeys {
private String alphabetLower;
private String alphabetUpper;
private String shiftedAlphabetLower1;
private String shiftedAlphabetUpper1;
private String shiftedAlphabetLower2;
private String shiftedAlphabetUpper2;
private int mainKey1;
private int mainKey2;
public CaesarCipherTwoKeys(int key1, int key2) {
alphabetLower = "abcdefghijklmnopqrstuvwxyz";
alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
shiftedAlphabetLower1 = alphabetLower.substring(key1) + alphabetLower.substring(0,key1);
shiftedAlphabetUpper1 = alphabetUpper.substring(key1) + alphabetUpper.substring(0,key1);
shiftedAlphabetLower2 = alphabetLower.substring(key2) + alphabetLower.substring(0,key2);
shiftedAlphabetUpper2 = alphabetUpper.substring(key2) + alphabetUpper.substring(0,key2);
mainKey1 = key1;
mainKey2 = key2;
}
public String encrypt(String input) {
StringBuilder encryptedInput = new StringBuilder(input);
OOCaesarCipher oocc1 = new OOCaesarCipher(mainKey1);
OOCaesarCipher oocc2 = new OOCaesarCipher(mainKey2);
for (int index=0; index < input.length(); index++) {
if (index % 2 == 0 || index == 0) {
encryptedInput.replace(index,index+1,oocc1.encrypt(input.substring(index,index+1)));
}
else {
encryptedInput.replace(index,index+1,oocc2.encrypt(input.substring(index,index+1)));
}
}
return encryptedInput.toString();
}
public String decrypt(String input) {
OOCaesarCipher oocc1 = new OOCaesarCipher(26-mainKey1);
OOCaesarCipher oocc2 = new OOCaesarCipher(26-mainKey2);
return oocc1.encrypt(input);
return oocc2.encrypt(input);
}
}
以下是上述代码中引用的OOCC类:
导入edu.duke。*;
public class OOCaesarCipher {
private String alphabetLower;
private String alphabetUpper;
private String shiftedAlphabetLower;
private String shiftedAlphabetUpper;
private int mainKey;
public OOCaesarCipher(int key) {
alphabetLower = "abcdefghijklmnopqrstuvwxyz";
alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
shiftedAlphabetLower = alphabetLower.substring(key) + alphabetLower.substring(0,key);
shiftedAlphabetUpper = alphabetUpper.substring(key) + alphabetUpper.substring(0,key);
mainKey = key;
}
public String encrypt(String input) {
StringBuilder encrypted = new StringBuilder(input);
for (int index=0; index < encrypted.length(); index++) {
Character currChar = encrypted.charAt(index);
int currentIndex = 0;
if (Character.isLowerCase(currChar)) {
currentIndex = alphabetLower.indexOf(currChar);
}
else {
currentIndex = alphabetUpper.indexOf(currChar);
}
if (currentIndex != -1 && Character.isLowerCase(currChar)) {
char newChar = shiftedAlphabetLower.charAt(currentIndex);
encrypted.setCharAt(index, newChar);
}
else if (currentIndex != -1) {
char newChar = shiftedAlphabetUpper.charAt(currentIndex);
encrypted.setCharAt(index, newChar);
}
}
return encrypted.toString();
}
public String decrypt(String input) {
OOCaesarCipher oocc = new OOCaesarCipher(26-mainKey);
return oocc.encrypt(input);
}
}
答案 0 :(得分:0)
对于您的解密方法,我相信您想返回原始输入字符串。要遵循代码的样式,您实际上可以使用26-key
从解密方法中调用crypto方法,如下所示:
public String decrypt(String input) {
mainKey1 = 26-mainKey1;
mainKey2 = 26-mainKey2;
String decrypted = CaeserCipherTwoKeys.encrypt(input);
// To maintain code consistency
mainKey1 = 26-mainKey1;
mainKey2 = 26-mainKey2;