0

だから私はJavaの割り当てのために書かれた "Caesar Cipher"を持っていますが、これまでのJavaの限られた知識では理解できない2つの必要なコンポーネントがありません.

出力された文字列を 4 文字のチャンクに分割して、出力をさらに暗号化する必要があります。つまり、AFSD GRTH WRGD

標準ライブラリからJavaメソッドを調べてみましたが、まだ正規表現を学んでおらず、これをメールで送信しているためグアバを使用できません。これもまだ授業で扱っていません。

また、plainText を暗号化するが、すべての可能なキー (0-25) に対して cipherText を出力する bruteForce メソッドを含める必要があります。shiftKey を 0 に設定し、ループの最後でキーを 1 ずつインクリメントする while ループを使用してみましたが、非常に混乱しました。

public String bruteForce(String plainText) {
    plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
    String cipherText = "";
    int shiftKey = 0;
    while (shiftKey <= 26) {
        for (int i = 0; i < plainText.length(); i++) {
            int charPosition = alphabet.indexOf(plainText.charAt(i));
            int keyVal = (shiftKey + charPosition) % 26;
            char replaceVal = this.alphabet.charAt(keyVal);
            cipherText += replaceVal;
            shiftKey++;
        }

    }
    return cipherText.toUpperCase();
}

これはこれまでの私のコードです

class CaesarCipher {
private final String alphabet = "abcdefghijklmnopqrstuvwxyz";
private final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public String bruteForce(String plainText, int shiftKey) {
    plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
    String cipherText = "";
    for (int i = 0; i < plainText.length(); i++) {
        int charPosition = alphabet.indexOf(plainText.charAt(i));
        int keyVal = (shiftKey + charPosition) % 26;
        char replaceVal = this.alphabet.charAt(keyVal);
        cipherText += replaceVal;
    }
    return cipherText.toUpperCase();
}

public String encrypt(String plainText, int shiftKey) {
    plainText = plainText.replaceAll("[^A-Za-z0-9]", "");
    String cipherText = "";
    for (int i = 0; i < plainText.length(); i++) {
        int charPosition = alphabet.indexOf(plainText.charAt(i));
        int keyVal = (shiftKey + charPosition) % 26;
        char replaceVal = this.alphabet.charAt(keyVal);
        cipherText += replaceVal;
    }
    return cipherText.toUpperCase();

}

public String decrypt(String cipherText, int shiftKey) {
    String plainText = "";
    for (int i = 0; i < cipherText.length(); i++) {
        int charPosition = this.ALPHABET.indexOf(cipherText.charAt(i));
        int keyVal = (charPosition - shiftKey) % 26;
        if (keyVal < 0) {
            keyVal = this.ALPHABET.length() + keyVal;
        }
        char replaceVal = this.ALPHABET.charAt(keyVal);
        plainText += replaceVal;
    }
    return plainText.toUpperCase();
}

public static void main(String args[]) {
    String plainText = "this; is s'ome te.xt with punct";
    int shiftKey = 4;

    CaesarCipher cc = new CaesarCipher();

    String cipherText = cc.encrypt(plainText, shiftKey);
    System.out.println("Plain  Text :" + plainText);
    System.out.println("Cipher Text :" + cipherText);
    String PlainText = cc.decrypt(cipherText, shiftKey);
    System.out.println("Plain Text  :" + PlainText);
}
}
4

1 に答える 1

1

4 つのチャンク

文字列を4つのチャンクに分割するために私が思いついたものは次のとおりです。

public  String[] splitIntoFour(String input){
    int chunkNum = 1+(input.length() / 4);
    String[] chunks = new String[chunkNum];
    for(int i = 0; i < chunkNum; i++){
        int startIndex = i*4;
        int endIndex = (i+1)*4;
        if(i == (chunkNum - 1))
            endIndex = input.length();
        chunks[i] = input.substring(startIndex, endIndex);
    }
    return chunks;
}

このメソッドは、文字列を受け取り、その長さを 4 で割り、4 で割り切れない場合に備えて 1 を加算します。各要素が 4 文字のチャンクになる文字列の配列を作成します。次に、チャンクごとに、入力の部分文字列を取得します。開始インデックスは 4 の倍数として計算され、終了インデックスも同様に計算されます。ループが最後の反復である場合を除き、その場合、終了インデックスは入力の長さとして計算されます。これは、4 で割り切れない入力を考慮するために行われます。

ブルートフォース法

総当たり法については、出力が26の異なる文字列であることを考えると、おそらく出力を配列としても提供したいでしょう。他の暗号化方法を使用して各キーで入力を暗号化するループを実行するだけです、それを正しく実装したことを考慮してください。

public String[] bruteForce(String plainText) {
    String[] cipherText = new String[26];
    for(int i = 0; i < cipherText.length; i++)
        cipherText[i] = encrypt(plainText, i);
    return cipherText;
}

ちなみに、ブルート フォース復号化は実際には簡単です。encrypt(String)ループ内のメソッドを独自のメソッドに変更するだけdecrypt(String)です。

public String[] bruteForceDecrypt(String cipherText) {
    String[] plainText = new String[26];
    for(int i = 0; i < plainText.length; i++)
       plainText[i] = decrypt(cipherText, i);
    return plainText;
}

配列の印刷

ブルート フォース メソッドを使用した 2 つの例を次に示します。どちらも 26 個の異なる文字列を出力します。

public static void main(String[] args){
    CaesarCipher cipher = new CeasarCipher();
    //This variable contains 26 strings, returned from the bruteForce() method
    String[] cipherText = cc.BruteForce("This is an example text");
    //For each string in cipherText, print it out
    for(String string : cipherText)
        System.out.println(string);
}

public static void main(String[] args){
    CaesarCipher cipher = new CeasarCipher();
    //This variable contains 26 strings, returned from the bruteForce() method
    String[] cipherText = cc.BruteForce("This is an example text");
    //Access each string in cipherText through it's index
    for(int i = 0; i < cipherText.length; i++)
        System.out.println(cipherText[i]);
}

これが役立つことを願っています。

于 2013-10-13T04:00:40.463 に答える