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

Java 使用抽象类构建简单的加密程序,java,encryption,class-structure,Java,Encryption,Class Structure,作为家庭作业,我的任务是创建一个程序,根据一种叫做围栏密码的方法对字符串进行加密。(我不知道这是否是常识,甚至你们是否想知道它到底是什么,所以如果你们也希望看到的话,我已经包括了维基百科) 我已经能够用直接的main方法成功地加密字符串。我现在使用类正确地编写代码 我不确定是否应该使用普通类创建对象。我的第一个倾向是使类抽象,简单地操作数据,并返回加密的方法。我真的不确定正确的方法是什么。我怀疑在抽象类或对象类中编写代码会有任何问题。我不确定哪一个被认为是正确的 有人能提供一些指导吗?我认为不使

作为家庭作业,我的任务是创建一个程序,根据一种叫做围栏密码的方法对字符串进行加密。(我不知道这是否是常识,甚至你们是否想知道它到底是什么,所以如果你们也希望看到的话,我已经包括了维基百科)

我已经能够用直接的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);
    }
}
公共最终类扰频器{
专用扰码器(){
}
/*
*使用三行围栏密码技术对字符串进行置乱
*
*/
公共静态字符串加扰三行(字符串_pText){
//接受消息字符串,删除空格,并使其小写
_pText=_pText.replaceAll(“[^a-zA-Z0-9]”,“”)。toLowerCase();
//获取纯文本字符串的长度,并标识其长度,
//然后是可被三除的下一个最高整数
int length=_pText.length()+(4-(_pText.length()%4));
//创建一个临时数组,用于填充
//将包含额外的空字符
char[]testext=\u pText.toCharArray();
//创建包含空字符的完整数组
char[]pText=新字符[长度];
//循环以填充完整数组
对于(int i=0;i
您的抽象类是否应该扩展?如果不是,那就不要抽象。不,我不会扩展它。我认为将其抽象化的唯一真正原因是我只处理数据。我脑海中看到的比较就像数学课上的“不创建对象”。创建
public static
方法,并将
构造函数标记为
private
,然后调用每个方法,如
Encrypt.encryptString(String String)
如果要创建实用程序类,则可以将其设置为
final
以避免扩展,并将其构造函数设置为private(避免实例化)。公平地说,仅将构造函数私有化也会使其不可扩展,因此我们可以跳过
final
关键字,但出于清晰的原因(比如
java.lang.Math
这样做),这样做很好。我编辑了我的帖子,其中包含了我根据您的建议尝试编写的代码。这是你的建议还是我完全搞砸了?你的抽象类应该扩展吗?如果不是,那就不要抽象。不,我不会扩展它。我认为将其抽象化的唯一真正原因是我只处理数据。我脑海中看到的比较就像数学课上的“不创建对象”。创建
public static
方法,并将
构造函数标记为
private
,然后调用每个方法,如
Encrypt.encryptString(String String)
如果要创建实用程序类,则可以将其设置为
final
以避免扩展,并将其构造函数设置为private(避免实例化)。公平地说,仅将构造函数私有化也会使其不可扩展,因此我们可以跳过
final
关键字,但出于清晰的原因(比如
java.lang.Math
这样做),这样做很好。我编辑了我的帖子,其中包含了我根据您的建议尝试编写的代码。这是你的建议还是我把事情搞砸了?