Question
So i'm trying to make this program and have the code pretty much done. The problem I'm having is that my code isn't generating a
So i'm trying to make this program and have the code pretty much done. The problem I'm having is that my code isn't generating a random string using the 16 bit key in my code. I'm suppose to have the string be random chracters and even which my code using String builder does but it's not working since the encryption and decryptin method should encrypt it and decrypt it. They aren't encrypting and decrypting any string in my code. I'm also suppose to have a brute force method which I have an idea how to implement but don't know a efficient way of implementing it.
My 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 empty="";
int xor;
char temp;
for(int i=0;i xor=msg.charAt(i)^key; temp=(char)xor; empty=empty+temp; } // This will return a encrypted message. msg=empty; return msg; } 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 empty=""; int xor; char temp; for(int i=0;i xor=msgE.charAt(i)^key; temp=(char)xor; empty=empty+temp; } msgE=empty; System.out.println(msgE); } private static void bruteForce(String msgE) { //TODO: implement bruteForce algorithm, you may need the above decryption(key,msgE) method decryption(key, msgE); boolean isEnglish=msgE.matches("[a-zA-Z]"); if(isEnglish) { System.out.println("This string is English: "+msgE); }else { System.out.println("This String is not English at all: "+msgE); } }}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started