2

对于任何不知道的人来说,这是凯撒的盒子加密

public class CaesarBox {

public static void main(String[] args) {
    // CaesarsBox <-encrypt|-decrypt>
    if (args[0].equals("-encrypt")) {
        System.out.println(encrypt(args[1]));
    } else if (args[0].equals("-decrypt")) {
        System.out.println(decrypt(args[1]));
    }
}

public static String encrypt(String plaintext) {
    // TODO put encryption code below this line
    plaintext = plaintext.replaceAll("\\s+", "");// removes white space
    plaintext = plaintext.toLowerCase();// converts capitol letters to lower
    // case

    char[] charArray = plaintext.toCharArray();
    // takes individual characters from the arguments and puts them into an
    // array

    int x = charArray.length; // assigns the length of charArray to x
    int y = 0;
    while (y < x) {
        ++y;
        if ((y == Math.floor(y)) && y * y >= x) {
        // tests if y is an integer
        // increases y until it is an integer
            break;
        }// above code finds the the length of the sides of the box
    }
    char[][] box = new char[y][y];// creates a 2d array
    int pos = 0;
    for (int i = 0; i < box.length; i++) {
        for (int j = 0; j < box[i].length; j++) {
            if (pos < plaintext.length()) {
                box[i][j] = plaintext.charAt(pos);
                pos++;
            // fills the array with the characters from the text to be
            // encrypted
            }
        } 
    }
    String encrypted = "";
    for (int i = 0; i < box.length; i++) {
        for (int j = 0; j < box.length; j++) {
            if (box[j][i] != 0) {// tells the program to ignore null values
                encrypted += box[j][i];
            }
            // prints out the letters in the box by column
        }

    }
    return encrypted;

// Put encryption code above this line

}

public static String decrypt(String cyphertext) {
    // TODO put decryption code below this line
    cyphertext = cyphertext.replaceAll("\\s+", "");// removes white space
    cyphertext = cyphertext.toLowerCase();// converts capitol letters to lower case

    char[] charArray = cyphertext.toCharArray();
    // takes individual characters from the arguments and puts them into an
    // array

    int x = charArray.length; // assigns the length of charArray to x
    int y = 0;
   while (y < x) {
        ++y;
        if ((y == Math.floor(y)) && y * y >= x) {
            // tests if y is an integer
            // increases y until it is an integer
            break;
        }// above code finds the the length of the sides of the box
    }
    char[][] box = new char[y][y];// creates a 2d array
    int pos = 0;
    for (int i = 0; i < box.length; i++) {
        for (int j = 0; j < box[i].length; j++) {
            if (pos < cyphertext.length()) {
                box[i][j] = cyphertext.charAt(pos);
                pos++;
            }
        }
        // fills the array with the characters from the text to be
        // encrypted
    }
    String decrypted = "";
    for (int i = 0; i < box.length; i++) {
        for (int j = 0; j < box[i].length; j++) {
            if (box[j][i] != 0) {// tells the program to ignore null values
                decrypted += box[j][i];
                // prints out the letters in the box by column
            }

        }
    }

    return decrypted;
    // Put decryption code above this line
    }

}

到目前为止,这是我的代码,我遇到的问题是解密不是完美正方形的东西,例如“javanoob 需要帮助”被加密为 joelaodpvbsanhnee,但被解密为 joseodaeepnlvhabn,这显然是不正确的。我知道解决方案是在正确的位置将空格放入数组中,但我不知道该怎么做,任何帮助都会很棒!:) 如果您需要我解释任何事情,请告诉我,谢谢!

4

2 回答 2

1

由于这条线,您正在失去空间:

plaintext = plaintext.replaceAll("\\s+", ""); // removes white space

您可能想要做的是从末端修剪空格而不是所有空格:

plaintext = plaintext.trim(); // Removes white space on ends.
于 2013-04-14T12:55:06.433 回答
0

对于我发现的凯撒盒子,字符总数的总和必须是一个完美的正方形。如果您在代码中尝试使用 25 个字符的消息,它可以正常工作:

The password is Bee ring twice

编码消息:

tsdethsirwewsiipobncarege

解码消息:

thepasswordisbeeringtwice

为了将消息放大到完美的字符数,您可以在框的空白处添加一个字符,例如:

for (int i = 0; i < box.length; i++) 
{
    for (int j = 0; j < box[i].length; j++) 
    {
        if (pos < plaintext.length()) 
        {
            box[i][j] = plaintext.charAt(pos);
            pos++;
        // fills the array with the characters from the text to be
        // encrypted
        }
        else 
            box[i][j] = 0;
    }
}

并修改 encriptes 字符串的构造,允许包含 0:

 for (int i = 0; i < box.length; i++) 
{
    for (int j = 0; j < box.length; j++) 
    { 
            encrypted += box[j][i];
        // prints out the letters in the box by column
    }

}
于 2013-04-14T13:53:56.447 回答