所以我有一个编程问题。我希望能够创建一个执行以下操作的breakCaesarCipher类:
1)拆分加密的消息
2)确定两个解密密钥
3)使用这些键创建CaesarCipherTwo实例。
4)调用解密方法。
我不知道从哪里开始。我对如何拆分加密邮件感到困惑。在上一课中,我们这样做了:
import edu.duke.*;
public class CaesarBreaker2 {
public int[] countOccurrencesOfLetters(String message) {
//snippet from lecture
String alph = "abcdefghijklmnopqrstuvwxyz";
int[] counts = new int[26];
for (int k=0; k < message.length(); k++) {
char ch = Character.toLowerCase(message.charAt(k));
int dex = alph.indexOf(ch);
if (dex != -1) {
counts[dex] += 1;
}
}
return counts;
}
public int maxIndex(int[] values) {
int maxDex = 0;
for (int k=0; k < values.length; k++) {
if (values[k] > values[maxDex]) {
maxDex = k;
}
}
return maxDex;
}
public String decrypt(String encrypted) {
CaesarCipher cc = new CaesarCipher();
int[] freqs = countOccurrencesOfLetters(encrypted);
int maxDex = maxIndex(freqs);
int dkey = maxDex - 4;
if (maxDex < 4) {
dkey = 26 - (4-maxDex);
}
return cc.encrypt(encrypted,26-dkey);
}
public String halfOfString(String message, int start) {
StringBuilder halfString = new StringBuilder();
for (int index=start;index < message.length();index += 2) {
halfString.append(message.charAt(index));
}
return halfString.toString();
}
public int getKey(String s) {
int[] letterFreqs = countOccurrencesOfLetters(s);
int maxDex = maxIndex(letterFreqs);
int dkey = maxDex - 4;
if (maxDex < 4) {
dkey = 26 - (4-maxDex);
}
return 26-dkey;
}
public String decryptTwoKeys(String encrypted) {
String firstHalfEncrypted = halfOfString(encrypted,0);
String secondHalfEncrypted = halfOfString(encrypted,1);
int firstHalfKey = getKey(firstHalfEncrypted);
int secondHalfKey = getKey(secondHalfEncrypted);
CaesarCipherTwo ccT = new CaesarCipherTwo();
System.out.println("First key:\t" + firstHalfKey + "\nSecond key:\t"
+ secondHalfKey);
return ccT.encryptTwoKeys(encrypted,firstHalfKey,secondHalfKey);
}
public void testDecrypt() {
FileResource fileResource = new FileResource();
String encrypted = fileResource.asString();
System.out.println("Encrypted message:\n" + encrypted);
System.out.println("\nDecrypted message:\n" + decryptTwoKeys(encrypted));
String encrypted2 = "Aal uttx hm aal Qtct Fhljha pl Wbdl. Pvxvxlx!";
System.out.println("Encrypted message:\n" + encrypted2);
System.out.println("\nDecrypted message:\n" + decryptTwoKeys(encrypted2));
}
}
但我不明白如何转换此代码,因此它将在这个名为TestCaesarCipherTwo的新类中工作。以下是TestCaesarCipherTwo的代码:
import edu.duke.*;
public class TestCaesarCipherTwo {
public int[] countOccurrencesOfLetters(String message) {
//snippet from lecture
String alph = "abcdefghijklmnopqrstuvwxyz";
int[] counts = new int[26];
for (int k=0; k < message.length(); k++) {
char ch = Character.toLowerCase(message.charAt(k));
int dex = alph.indexOf(ch);
if (dex != -1) {
counts[dex] += 1;
}
}
return counts;
}
public int maxIndex(int[] values) {
int maxDex = 0;
for (int k=0; k < values.length; k++) {
if (values[k] > values[maxDex]) {
maxDex = k;
}
}
return maxDex;
}
public String halfOfString(String message, int start) {
StringBuilder halfString = new StringBuilder();
for (int index=start;index < message.length();index += 2) {
halfString.append(message.charAt(index));
}
return halfString.toString();
}
public void simpleTests() {
FileResource fileResource = new FileResource();
String fileAsString = fileResource.asString();
CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(17, 3);
String encrypted = cctk.encrypt(fileAsString);
System.out.println("Encrypted string:\n"+encrypted);
String decrypted = cctk.decrypt(encrypted);
System.out.println("Decrypted string:\n"+decrypted);
String blindDecrypted = breakCaesarCipher(encrypted);
System.out.println("Decrypted string using breakCaesarCipher():\n"+blindDecrypted);
}
public String breakCaesarCipher(String input) {
int[] freqs = countOccurrencesOfLetters(input);
int freqDex = maxIndex(freqs);
int dkey = freqDex - 4;
if (freqDex < 4) {
dkey = 26 - (4-freqDex);
}
CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey);
return cctk.decrypt(input);
}
}
警告:此行上也有构造函数错误CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey);在类CaesarCipherTwoKeys中声明CaesarCipherTwoKeys不能应用于给定类型; required int,int;找到了......
任何人都可以告诉我如何更改我的代码,以便我的breakCaesarCipher方法拆分加密的消息,确定用于加密消息的两个解密密钥,用这两个密钥创建CaesarCipherTwo的实例,并调用解密方法?欢迎任何建议。如果您想了解更多有关此问题性质的详细信息,请与我们联系。