此Play Fair Cipher具有一个包含81个字符的9x9矩阵。当我找到一个文件和/或text.doc并运行它并尝试加密它时,我一直遇到的问题是一个nullpointer异常。另一个例外是当我在JTextArea(结果)中键入一定数量的单词/字符时,我得到一个超出界限的字符串。最后,在我执行了两个操作之后,我得到一个数组超出范围的异常。加密和解密方法都有些类似。唯一可行的是当我尝试加密和解密少量单词时,例如(你好我的名字是John,Jerry。我的名字是......等等)。此代码是程序的加密部分。有人可以帮助解决这个问题。我非常感谢。
String keyword = phraseTF.getText();
// /this loop grabs the string from the JTextfield and initializes it and removes any repeated letter and or symbols*****
String CONST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '()*+,-./:;<=>[]^?";
keyword = keyword + CONST;
String distinctive = "";
for (int i1 = 0; i1 < keyword.length(); i1++) {
if (distinctive.indexOf(keyword.charAt(i1)) == -1) {
distinctive = distinctive + keyword.charAt(i1);
}
}
System.out.println("Distinctive " + distinctive);
// /this loop inserts the distinctive String into 9x9 matrix********
char[][] playfairTable = new char[9][9];
String Storage;
int frist = 0;
int ninth = 9;
for (int i1 = 0; i1 < 9; i1++) {
Storage = distinctive.substring(frist, ninth);
for (int i11 = 0; i11 < 9; i11++) {
Storage = distinctive.substring(frist, ninth);
playfairTable[i1][i11] = Storage.charAt(i11);
System.out.println("Storage [" + i1 + "][" + i11 + "] "+ playfairTable[i1][i11]);
}
frist = frist + 9;
ninth = ninth + 9;
}
// /this loop encrypts the results from the JTextArea using the secret phrase from the JTextField***********************
String text = result.getText();
System.out.print("READING IN: "+text+"\n");
text=text.replace("\n", "@");
String check = text.replace("@", "");
if(check.length()%2==1){
System.out.println("String Parity: odd\n");
text+=" ";
}
int ind = 0; // /main int***
String ans = "";
while (ind < text.length()) {
boolean flag= false;
boolean flag2= false;
char i1 = text.charAt(ind);
ind++;
if(i1=='@'){
flag =true;
i1 = text.charAt(ind);
ind++;
}
char i2 = text.charAt(ind);
ind++;
if(i2=='@'){
flag2= true;
i2 = text.charAt(ind);
ind++;
}
System.out.println("Character pair is: '" + i1 + "', '" + i2 + "'");
char a = i1;
char b = i2;
int[] tmp = findRowCol(a, playfairTable);
int a_r = tmp[0], a_c = tmp[1];
tmp = findRowCol(b, playfairTable);
int b_r = tmp[0], b_c = tmp[1];
char at='@';
char O1 = 0, O2 = 0;
boolean flag1 = false, flagtwo=false, flag3= false;
//The event statements for the Play Fair Cipher*****************************************************
if (a == b){
O1=a;
O2=b; // d. ***two characters are the same they do not change.
}
else if (a_r == b_r) { // b. ***same row
O1=playfairTable[a_r][(a_c + 1) % 9];
O2=playfairTable[b_r][(b_c + 1) % 9];
} else if (a_c == b_c) { // c. ***same column
O1=playfairTable[(a_r + 1) % 9][a_c];
O2=playfairTable[(b_r + 1) % 9][b_c];
}
else if (text.length() < 2) {// e. ***text character will be the same if the text is odd****************************
ans = ans + a;
System.out.println("at e. else if");
} else if (((a_r != b_r) && (a_c != b_c))) {// a. ***if the Inputs are in the corners
O1=playfairTable[a_r][b_c];
O2= playfairTable[b_r][a_c];
}
///////3 boolean flags ////Okay I think its the if-else statements, you need 3 checks, plus a last else, I think.
if(flag1==true){
ans=ans+'@'+O1+O2;
}
if(flagtwo==true){
ans=ans+O1+'@'+O2;
}
else if(flag3==true){
ans+=i1;
}
else {ans=ans+O1+O2;}
ans = ans.replace("@", "\n");
result.setText(ans); //the output of the combination
System.out.println("ANS=" + ans);
}
}
////This method looks and stores the characters*****
private int[] findRowCol(char a, char[][] playfair) {
for (int r = 0; r < 9; r++)
// /row
for (int c = 0; c < 9; c++)
// //column
if (playfair[r][c] == a)
return new int[] { r, c };
return null;
}
答案 0 :(得分:0)
如下所述,可能会引起一些例外。
<强>的NullPointerException:强>
findRowCol
如果在a
数组中找不到字符playfair
,则返回null,例如如果text
包含81个字符中未找到的某些字符,例如`〜etc. <强>的StringIndexOutOfBoundsException:强>
String.substring(from,to)
可能会导致此异常。请尝试以下代码填充playfairTable
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
playfairTable[i][j] = distinctive.charAt(i*9+j);
}
}
检查无效字符:
for (int i1 = 0; i1 < keyword.length(); i1++) {
if (CONST.indexOf(keyword.charAt(i1)) == -1) {
System.out.println("Invalid char");
return;
}
}