Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

}

}}

Programming Assignment 1 Implement a toy symmetric cryptosystem based on the following method. Keys are 16-bit randomly generated values. Messages are randomly generated strings with an even number of characters. (Valid characters are upper and lower case letters, as well as spaces.) One can always add a blank at the end of an odd-length string. The encryption of a message M of length n (in bytes) is given by a. b. C. where the key K is repeated n/2 times. "II" here is String Concatenation Operator. d. The decryption algorithm for a ciphertext C is the same as the encryption algorithm D (C)-C(KIKIIK Implement a brute-force decryption attack for this cryptosystem and test it on randomly generated English character message. Automate the process of detecting whether a decrypted message is English characters. Graduate students task and bonus task for undergraduate students The randomly generated message has to be an English words message rather than English character message. That means you have to use English words dictionary in your program. Thus the brute force will continue until find an English words message

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions