gpt4 book ai didi

java - 凯撒之盒加密/解密程序

转载 作者:行者123 更新时间:2023-11-30 11:33:49 24 4
gpt4 key购买 nike

那是一个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

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

plaintext = plaintext.trim(); // Removes white space on ends.

关于java - 凯撒之盒加密/解密程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com