简体   繁体   中英

Can someone help me figure out what may be causing error exceptions in my PlayFair Cipher?

This Play Fair Cipher has a 9x9 matrix with 81 characters. The problem that I keep getting is a nullpointer exception when I find a file and/or text.doc and run it and try to encrypt it. The other exception is when I type in a certain number of word/characters within the JTextArea (results) and I get a String out of bounds. Lastly after I do either both actions I get an Array out of bounds exception. Both encrypt and decrypt methods are somewhat similar. The only thing that does work is when I try to encrypt and decrypt only a small number of words, example (Hello my name is John, Jerry. My name is......etc). This code is the encryption portion of the program. Can someone help figure this out. I would greatly appreciate that.

          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;
                }

It may cause some exception as mentioned below.

NullPointerException:

  • Method findRowCol return null if character a is not found in playfair array for eg if text contains some characters that is not found in your 81 characters such as ` ~ etc.

StringIndexOutOfBoundsException:

  • Method String.substring(from,to) can cause this exception.
  • Try below code to populate playfairTable

     for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ playfairTable[i][j] = distinctive.charAt(i*9+j); } } 

Check for invalid characters:

    for (int i1 = 0; i1 < keyword.length(); i1++) {
        if (CONST.indexOf(keyword.charAt(i1)) == -1) {
            System.out.println("Invalid char");
            return;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM