gpt4 book ai didi

java - 使用 CaesarCipherBreaker 拆分字符串

转载 作者:行者123 更新时间:2023-12-01 10:36:33 24 4
gpt4 key购买 nike

我如何向此示例添加代码以创建一个 CaesarCipherBreaker 方法,该方法用两个 key 分割加密消息。到目前为止我已经写了这么多:

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 in class CaesarCipherTwoKeys cannot be applied to given types; required int,int; found int...

我现在的breakCaesarCipher方法只能计算出一个 key ,而不是两个。我应该如何编写一个方法来分割加密字符串,找出用于解密的两个 key 。

最佳答案

如果我正确理解您的代码,您只需调用 halfOfString (两次)即可获取密文的两部分,然后使用通常的方法来破解这两个部分的 Ceaser-Cipher分别。

您的错误似乎是由于双 key 加密需要(毫不奇怪)两个 key 这一事实造成的。您应该将它们都交给构造函数。

public String breakCaesarCipher(String input) {
String in_0 = halfOfString(input, 0);
String in_1 = halfOfString(input, 1);
// Find first key
// Determine character frequencies in ciphertext
int[] freqs_0 = countOccurrencesOfLetters(in_0);
// Get the most common character
int freqDex_0 = maxIndex(freqs_0);
// Calculate key such that 'E' would be mapped to the most common ciphertext character
// since 'E' is expected to be the most common plaintext character
int dkey_0 = freqDex_0 - 4;
// Make sure our key is non-negative
if (dkey_0 < 0) {
dkey_0 = dkey_0+26;
}
// Find second key
int[] freqs_1 = countOccurrencesOfLetters(in_1);
int freqDex_1 = maxIndex(freqs_1);
int dkey_1 = freqDex_1 - 4;
if (freqDex_1 < 4) {
dkey_1 = dkey_1+26;
}

CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey_0, dkey_1);
return cctk.decrypt(input);
}

关于java - 使用 CaesarCipherBreaker 拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34688725/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com