使用抽象类构建简单加密程序

时间:2016-02-05 00:22:21

标签: java encryption class-structure

对于家庭作业,我被分配了创建程序的任务,该程序基于称为Rail Fence Cipher的方法加密String。 (我不知道这是否是常识,或者即使你们想要确切地知道它是什么,所以如果你愿意的话,我已经包括了维基百科link

我已经能够在一个直接的main方法中成功加密字符串。我现在正在使用类正确编写代码。

我不确定是否应该使用普通类并创建对象。我的第一个倾向是使类抽象,并简单地操纵数据,并返回加密的方法。我不确定这是怎么回事。我怀疑在抽象类或对象类中编写代码会有任何问题。我不确定哪个被认为是正确的。

有人可以提供任何指导吗?我看到不使用抽象类的最大原因是该程序很简单,不使用任何类型的接口。

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

0 个答案:

没有答案