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