对于任何不知道的人来说,这都是Caesar's box encryption
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,这显然是不正确的。我知道解决方案与在正确的位置将空间放入数组有关,但是我不知道该怎么做,任何帮助都将是惊人的! :)如果您需要我解释任何事情,请告诉我,谢谢!
最佳答案
由于此行,您正在丢失空格:
您可能想要做的是从末端修剪空间,而不是修剪所有空间:
plaintext = plaintext.replaceAll("\\s+", ""); // removes white space