それは知らない人のためのシーザーズボックス暗号化です
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
}
}
これまでのところ私のコードです。私が抱えている問題は、完全な正方形ではないものを解読することです。解決策は、配列の適切な場所にスペースを入れることと関係があることは知っていますが、その方法がわかりません。どんな助けでも素晴らしいでしょう! :) 何か説明が必要な場合は、お知らせください。