Java 如何在不重复的情况下将英文字符存储在数组中(尝试解决Play Fair Cypher加密)?(爪哇)
我正试图解决java编程语言中的公平加密问题。因此,算法首先将(键)元素存储在5*5数组中,然后存储除键中包含的字符以外的其他字符 例如,如果KEY=“LACOR”,则数组必须如下所示:Java 如何在不重复的情况下将英文字符存储在数组中(尝试解决Play Fair Cypher加密)?(爪哇),java,arrays,encryption,Java,Arrays,Encryption,我正试图解决java编程语言中的公平加密问题。因此,算法首先将(键)元素存储在5*5数组中,然后存储除键中包含的字符以外的其他字符 例如,如果KEY=“LACOR”,则数组必须如下所示: a[0][0]=L a[0][1]=A a[0][2]=C a[0][3]=O a[0][4]=R a[1][0]=B a[1][1]=D a[1][2]=E a[1][3]=F a[1][4]=G .... a[4][4]=Z 我将“KEY”存储在数组中,但我不知道如何在该数组中存储其余字符而不重复。 有人
a[0][0]=L
a[0][1]=A
a[0][2]=C
a[0][3]=O
a[0][4]=R
a[1][0]=B
a[1][1]=D
a[1][2]=E
a[1][3]=F
a[1][4]=G
....
a[4][4]=Z
我将“KEY”存储在数组中,但我不知道如何在该数组中存储其余字符而不重复。
有人告诉我这样做的想法吗
Java代码:
public class playFair {
public static void main(String[] args){
String plain, key;
plain = "BALLOON";
key = "MONARCHY";
int c=0;
char matrix[][] = new char[5][5];
char keyToArray [] = key.toCharArray();
/*char alphabetic[] = new char[27];
for(char i='A'; i<'Z'+1; i++){
alphabetic[c] = i;
c++;
//System.out.println("["+i+"]"+alphabetic[c]);
}
c=0;*/
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix.length; j++){
if(c < keyToArray.length){
matrix[i][j] = keyToArray[c];
c++;
}
else{
}
System.out.println("["+i+"]["+j+"] = "+matrix[i][j]);
}
}
}
}
公共类游乐场{
公共静态void main(字符串[]args){
弦素,键;
plain=“气球”;
key=“君主制”;
int c=0;
字符矩阵[][]=新字符[5][5];
char keyToArray[]=key.toCharArray();
/*字符字母[]=新字符[27];
对于(char i='A';i我建议您采用以下方法:
//the result
char [][]a = new char[5][5];
//the alphabet
char [] alphabet = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z'};
//the key
String key = "LACOR";
//use count as effective index of the array
int count = 0;
//fill the result with the key
for(;count<key.length();count++){
a[count/5][count%5]=key.charAt(count);
}
//fill the rest of the array with alphabet - key
for(int i = 0;i<alphabet.length;i++){
if(!key.contains(""+alphabet[i])){
a[count/5][count%5]=alphabet[i];
count++;
}
}
//print the result
System.out.println(Arrays.deepToString(a));
//结果
字符[][]a=新字符[5][5];
//字母表
字符[]字母表=新字符[]{'A'、'B'、'C'、'D'、'E'、'F'、'G'、'H'、'I'、'J'、'K'、'L'、'M'、'N'、'O'、'P'、'Q'、'R'、'S'、'T'、'U'、'V'、'X'、'Y'、'Z'};
//钥匙
String key=“LACOR”;
//使用计数作为数组的有效索引
整数计数=0;
//用键填充结果
对于(;count我建议您采用以下方法:
//the result
char [][]a = new char[5][5];
//the alphabet
char [] alphabet = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','Z'};
//the key
String key = "LACOR";
//use count as effective index of the array
int count = 0;
//fill the result with the key
for(;count<key.length();count++){
a[count/5][count%5]=key.charAt(count);
}
//fill the rest of the array with alphabet - key
for(int i = 0;i<alphabet.length;i++){
if(!key.contains(""+alphabet[i])){
a[count/5][count%5]=alphabet[i];
count++;
}
}
//print the result
System.out.println(Arrays.deepToString(a));
//结果
字符[][]a=新字符[5][5];
//字母表
字符[]字母表=新字符[]{'A'、'B'、'C'、'D'、'E'、'F'、'G'、'H'、'I'、'J'、'K'、'L'、'M'、'N'、'O'、'P'、'Q'、'R'、'S'、'T'、'U'、'V'、'X'、'Y'、'Z'};
//钥匙
String key=“LACOR”;
//使用计数作为数组的有效索引
整数计数=0;
//用键填充结果
计数