-1

I'm practicing my java skills and I pretty much have the program done but I'm having couple problems with the code. So the problem with the code that I'm having is that I'm getting Nullpointexceptions and can't figure out where that is.

Secondly, I'm trying to implement a brute-force method in my code but I'm having problems with implementing it efficiently. For the method, I have to see if the String is in English which I have done but base on other codes of using brute force method. I can use the method in many ways but don't know efficiently implementing it in my code.

So my program does the following:

  • Implement a symmetric cryptosystem.
  • Generate a 16 bit value key
  • Messages are randomly generated string with an even number of characters.
  • Encrypt and Decrypt a method.
  • Implement a brute force method decryption of attack for this cryptosystem using the randomly generated String.
  • Also I can only call methods in Main

    Error: Exception in thread "main" java.lang.NullPointerException at StudentSystem.bruteForce(StudentSystem.java:112) at StudentSystem.main(StudentSystem.java:38)

Code:

private static String msg;

                private static String msgE;

                private static String msgD;

                private static int key;
                //This is the String for the program. So Upper is the chracters in upper letters
                private static String upper="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                //This is for the chracters in the lower case
                private static String lower=upper.toLowerCase();
                private static String space=" ";
                //Alphanum is upper and lower and space combined and this is what I'll be using for this project. 
                private static String alphanum=upper+lower+space;


                public static void main(String[] args){

                //TODO: You can only call methods in main method

                key = generateKey();

                msg = generateMsg();

                msgE = encryption(key,msg);

                bruteForce(msgE);

                }

                private static int generateKey() {

                //TODO: implement step a (randomly generate 16-bit key)
                    //So in Decimal a key would be 2^16-1 
                    //So i'm using random rand to generate a key which is going to be a int and in power of 2^16 -1
                    Random rand=new Random();
                    return rand.nextInt((int) (Math.pow(2, 16)-1));


                }

                private static String generateMsg() {

                //TODO: implement step b (randonly generate a string with an even number of characters)
                    //For this method i'm going to generate a string which is random 
                    //This string is going to use the above alphanum.
                StringBuilder builder =new StringBuilder();
                //The while loop states while the builder length is divisible by 2 then you can print the random string

                while(builder.length()%2!=0) {
                    //The chracter is using the random rand and alphanum length to generate the String
                    int character=(int)(Math.random()*alphanum.length());
                    //Builder append is shortering the string into something more simple. 
                    builder.append(alphanum);
                }

                return builder.toString() ;

                }

                private static String encryption(int key, String msg) {

                //TODO: implement step c (encrypt the message)
                    //To encrypt the string we're going to use the key we generated before
                    /*The String Key is going to take the key and put that into a string*/
                    //then the loop is going to go through and put the String Key to generate a key
String Key=Integer.toString(key);
for(int i=0;i<=msg.length()/2;i++) {
    Key+=Key;
}
// This will return a mesage and a key at the same time
                return (msg+key);

                }

                private static void decryption(int key, String msgE) {

                //TODO: implement step d (decryption)
                    //For decryption we're going to use the key we got before and go through the loop
                    //We're going to go through the loop and put the String into String Key
                    //Then we're going to return the String with the key.
                    String Key=Integer.toString(key);
                    for(int i=0;i<msgE.length()/2;i++) {
                        Key+=Key;
                    }
                    String msgD;
                    msgD=msgE+key;

                }

                private static void bruteForce(String msgE) {

                //TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method
                    boolean isEnglish=msgE.matches("[a-zA-Z]+");
                    if(isEnglish) {
                    System.out.println("This string is English");
                    }else {
                        System.out.println("This String is not English at all");
                    }
                    decryption(key,msgE);
                    boolean isEnglish2=msgD.matches("[a-zA-Z]");
                    if(isEnglish2) {
                        System.out.println("Encrypted Message is English: "+msgD);
                    }
                    else {
                        System.out.println("The message is not english at all: "+msgD);
                    }
                }
        }
zaph
  • 111,848
  • 21
  • 189
  • 228
black
  • 59
  • 2
  • 11

1 Answers1

0

You're confusing class fields with method variables by giving them the same name. The NullPointerException is because you never initialize msgD, the class field.

So here, you declare a field msgD:

private static String msgD;

but then in the decryption method, you declare a new, local variable with the same name:

String msgD;
msgD=msgE+key;

Any time you put the class of a variable before its name, you are declaring a new variable. I repeat: this is a new local variable, which has no relation to your previously-declared field, even though they have the same name.

So, after you run the decryption method, you're expecting msgD to have a value, but it doesn't, throwing an NPE when you try to reference it on this line:

boolean isEnglish2=msgD.matches("[a-zA-Z]");

If you remove the line

String msgD;

from the decryption method, the NPE should go away, but I suspect there may be other problems lurking.

nvioli
  • 4,137
  • 3
  • 22
  • 38