1

宿題として、Rail Fence Cipher と呼ばれる方法に基づいて文字列を暗号化するプログラムを作成するタスクが割り当てられました。(これが一般的な知識であるかどうか、またはあなたがそれが何であるかを正確に知りたいかどうかさえわからないので、もしあなたが見たい場合はウィキペディアのリンクを含めました).

ストレートなメイン メソッドで文字列を正常に暗号化できました。クラスを使用してコードを正しく記述できるようになりました。

通常のクラスを使用してオブジェクトを作成する必要があるかどうかはわかりません。私の最初の傾向は、クラスを抽象化し、単純にデータを操作して、暗号化されたメソッドを返すことです。これについて正しい方法が何であるかはよくわかりません。抽象クラスまたはオブジェクトクラスでコードを書くのに問題があるとは思えません。どちらが正しいと考えられるかはわかりません。

誰でもガイダンスを提供できますか?抽象クラスを使用しない最大の理由は、このプログラムが単純であり、いかなる種類のインターフェースも使用しないことです。

    public final class Scrambler {

private Scrambler() {

}

/*
*Scrambles a string using a three line rail fence cipher technique 
*
 */
public static String scrambleByThreeLines(String _pText) {

    //takes the message string, removes spaces, and makes lowercase
    _pText = _pText.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

    //Takes the length of the plain text string, and identifies how long it is,
    //then the next highest integer that is divisble by three
    int length = _pText.length() + (4 - (_pText.length() % 4));

    //creates a temporary array to use when populating the actual array that
    //will contain the extra null characters
    char[] tempText = _pText.toCharArray();

    //creates what will be the full array including the null characters
    char[] pText = new char[length];

    //loop to populate full array
    for (int i = 0; i < pText.length; i++) {
        if (i < tempText.length) {
            pText[i] = tempText[i];
        } else {
            pText[i] = 'x';
        }
    }
    /*Scrambling function of the cipher-------------------------------------
    *creates four strings that are concatenated togeather with a space inbetween
    *utalizing the three rail cipher technique as shown on Page 13 of the 
    *Martin Gardner book, "Codes Ciphers, and Secret Writings"
    */
    String one = "";
    String two = "";
    String three = "";
    String four = "";
    int counter = 0;
    for (int i = 0; i < pText.length; i++) {
        if ((i + 1) % 2 == 0) {
            if (counter % 2 == 0) {
                three += pText[i];
            } else {
                two += pText[i];
            }

        } else {
            if (counter % 2 == 0) {
                one += pText[i];
            } else {
                four += pText[i];
            }
            counter++;
        }
    }
    return String.format(one + " " + two + " " + three + " " + four);
}

/*
*Unscrambles a string using a three line rail fence cipher technique 
 */
public static String unscrableByThreeLines(String _cText) {
    //splits the text into the four strings to decode-----------------------
    String[] a = _cText.split(" ", 4);

    char[] one = a[0].toCharArray();
    char[] two = a[1].toCharArray();
    char[] three = a[2].toCharArray();
    char[] four = a[3].toCharArray();
    //----------------------------------------------------------------------
    //removes one char at a time in the correct order to decode to a String
    String pText = "";

    for(int i =0; i < one.length; i++){
        pText += one[i];
        pText += two[i];
        pText += four[i];
        pText += three[i];
    }

     return String.format(pText);
    }
}
4

0 に答える 0