用CaesarCipherBreaker拆分字符串 [英] Splitting a string with CaesarCipherBreaker

查看:113
本文介绍了用CaesarCipherBreaker拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为此示例添加代码,以创建一个CaesarCipherBreaker方法,该方法将加密的邮件分为两个键。到目前为止,我已经写下了这么多的东西:

How would I add code to this example for creating a CaesarCipherBreaker method that splits the encrypted message by two keys. So far I have this much written down:

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中的CaesarCipherTwoKeys不能应用于给定的类型; CaesarCipherTwoKeys(CaesarCipherTwoKeys(dkey); 说明 CaesarCipherTwoKeys cctk =需要int,int;发现int ...

WARNING: I also have a constructor error on this line CaesarCipherTwoKeys cctk = new CaesarCipherTwoKeys(dkey); stating CaesarCipherTwoKeys in class CaesarCipherTwoKeys cannot be applied to given types; required int,int; found int....

breakCaesarCipher方法现在只显示出一个键,而不是两个。我应该如何编写一个分割加密字符串的方法,找出用于解密的两个密钥。

The breakCaesarCipher method I have now only figures out one key, not two. How should I go about writing a method that splits an encrypted string, figuring out two keys used for decryption.

推荐答案

如果我正确理解你的代码,你可以直接调用你的 halfOfString (两次)获得密文的两个部分,然后使用通常的方法分别在两个部分上打破一个Ceaser-Cipher。

If I understand your Code correctly, you could just call your halfOfString (two times) to get the two parts of the ciphertext and then use your usual approach to breaking a Ceaser-Cipher on both parts separately.

您的错误似乎结果从两键加密(不出意外)两个键的事实。你应该给他们两个构造函数。

Your error seems to result from the fact that the two-key-encryption expects (unsurprisingly) two keys. You should give them both to the constructor.

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);
}

这篇关于用CaesarCipherBreaker拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆