如何使用2d数组对字符串进行加密和解密



我实际上使用2d数组加密和解密了纯字符串。

下面是我的加密代码:

import java.util.Scanner;
public class Secret {
public static void  main(String[] args) {
Scanner x = new Scanner(System.in);

System.out.println("Enter a sentence that need to be encrypt : ");
String y = x.nextLine();

int len = y.length();

System.out.println("The length of the sentences is : " + len);

System.out.print("Enter number of column : " );
int arr = x.nextInt();  

char [] [] z = new char [6] [arr];

// fill the character into array //
int pos = 0;
for (int i = 0; i<z.length; i++) {
for (int j = 0; j<z[i].length; j++) {
z[i][j] = y.charAt(pos);
pos++;

}
}
// output the encrypted text by reading the array downward, column by
column.//
for (int j = 0; j<z[arr].length; j++) {
System.out.print(z[0][j] + "" + z[1][j] + "" + z[2][j] + "" + z[3][j] + "" + z[4][j] 
+ "" + z[5][j]);}   
}
}

输入:

Enter a sentence that need to be encrypt : 
Hello World.
The length of the sentences is : 12
Enter number of column : 2

输出:

HloWrdel ol. 

但我想要的输出类似于HloWrdel*ol...,这意味着空间被"*"替换,如果有额外的空数组,则打印"."

现在,我的数组大小取决于字符串长度,所以当我输入arr=3(6X3=18(时,它会显示类似的错误

Enter a sentence that need to be encrypt : 
Hello World.
The length of the sentences is : 12
Enter number of column : 3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret.main(Secret.java:33)

因此,我的问题是如何得到像HloWrdel*ol...这样的期望输出?空间变为"*",并且输入阵列大小没有限制。当我输入大于字符串长度的数组大小(列(时,字符串的字符会自动填充数组,多余的空数组将打印"."

对于解密代码:

import java.util.Scanner;
public class Secret1 {

public static void main(String[] args) {

Scanner x = new Scanner(System.in);

System.out.println("Enter the sentences that need to decrypt : ");
String str = x.nextLine();

int len = str.length();

System.out.println("The number of length is " + len);

System.out.println("Enter the number of rows : " );
int arr = x.nextInt();

char [][] z = new char [arr][6];

// fill character into array //
int pos = 0;
for(int i = 0; i<z.length; i++) {
for(int j = 0; j<z[i].length; j++) {
z[i][j] = str.charAt(pos);
pos++ ;
}

}

//output the decrypted text by reading the array from left to right, row by
row.//
for (int i = 0; i<z.length; i++) {
System.out.print(z[i][0]);
}

for (int i = 0; i<z.length; i++) {
System.out.print(z[i][1]);
}

for (int i = 0; i<z.length; i++) {
System.out.print(z[i][2]);
}

for (int i = 0; i<z.length; i++) {
System.out.print(z[i][3]);
}

for (int i = 0; i<z.length; i++) {
System.out.print(z[i][4]);
}

for (int i = 0; i<z.length; i++) {
System.out.print(z[i][5]);
}
}

}

输入:

Enter the sentences that need to decrypt : 
HloWrdel ol.
The number of length is 12
Enter the number of rows : 
2

输出:

Hello World.

但我想要的输出是从加密文本HloWrdel*ol...转换为纯文本Hello World.。我怎样才能得到这样的输出?

此外,解密代码也取决于字符串长度,所以当我输入的数组大小大于字符串长度时,它显示如下:

Enter the sentences that need to decrypt : 
HloWrdel ol.
The number of length is 12
Enter the number of rows : 
3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:711)
at Secret1.main(Secret1.java:147)

我输入的数组大小是否可能不限于字符串长度,而是在填充数组后不为多余的空数组打印任何内容?

在最里面的循环(for (int j = 0; j<z[i].length; j++)(中,检查是否为pos >= len,如果是,则跳过读取并用''填充2D元素的尝试。'

if (pos>=len)
{
z[i][j]='.';
continue;
}
//your normal code

然后在你填充元素后,检查你是否刚刚填充了空间

if (z[i][j]==' ')
z[i][j]='*';

最新更新