在显示纯文本时,我在Scytale密码中遇到了麻烦,在行和列中缺少时,也遇到了“ z”字母。
为了使说明更容易,我希望这样显示明文:
这是我的代码:
import java.util.*;
public class scytale{
public static String scytaleEncode(String plaintext, int numRows){
String encodedText = "";
if(numRows>=plaintext.length() || numRows <=0){
System.out.println(plaintext + "z");
}
else{
while(plaintext.length()%numRows != 0){
plaintext += " ";
}
int numCols = plaintext.length()/numRows;
for(int i = 0; i<numCols; i++){
for (int y = i; y <plaintext.length(); y+=numCols;){
encodedText += plaintext.charAt(y);
}
System.out.println();
}
return encodedText;
}
}
public static void main(String[] args){
System.out.println("-------WELCOME TO SCYTALE CIPHER-------");
String cont;
do{
Scanner sc = new Scanner(System.in);
System.out.println("=================================");
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.nextLine();
System.out.println("\nEnter your key: ");
int key;
key = sc.nextInt();
System.out.println("\nCiphered:" + scytaleEncode(message.replaceAll("\\s",""), key));
System.out.println("\nWould you like to continue? (y/n): ");
cont = sc.next();
System.out.println("\n");
}while(cont.equalsIgnoreCase("y"));
System.out.println("Thank you for using SCYTALE CIPHER!\n");
}
}
我应该怎么做?