Question

In: Computer Science

For this question, you need to implement Java code for the following Modified Ciphertext encryption and...

For this question, you need to implement Java code for the following Modified Ciphertext encryption and attack for known patterns to find the code.

Please read the modified Ciphertext carefully and examples before implementing them.  

Modified Ciphertext is working as follows:

If a plain text (only letters ignore uppercase or lowercase) and sequences of numbers are given, it encrypts the given plain text by shifting each letter in a message the characters based on the given numbers one forward direction and one backward direction in order.

Example: consider the repeating key: 3 1 7 4 2 5 6, and plain text is "Hello World"

The encoded message will be: "Kdshq Ruomw"

H->3 = K, e<-1= d, l->7 = s, l<-4 = h, o->2 = q, W<-5 = R, o->6 = u, r<-3 = o, l->1 = m, d<-7 = w.

First, implement an encryption method:

"String encryption(String message, String code)" method that returns the encoded message according to the given message and code.

Ex: encryption( "Hello World", "3174256 ") will return "Kdshq Ruomw"

Second, implement an attack method:

"String attack(String message, String encoded_message)" method that returns the predicted code without knowing back or forward movements.

You may assume that every digit in the code is distinct.

Ex: attach( "Hello World", " Kdshq Ruomw") will return "3174256"


Please submit a Java code for two methods and JUnit tests that show both methods work for different cases, including exception cases such as non-digit for code or non-plain text.

Solutions

Expert Solution

An exception is a condition that is caused by a run-time error in the program.The purpose of exception handling mechanism is to provide a means to detect and report an exceptional circumstance.The error handling code that performs the following task:

1. Find the problem (Hit the exception).

2. Inform that an error has occurred(Throw the exception).

3.Receive the error information(Catch the exception).

4.Take corrective actions (Handle the exception).

The error handling code basically consists of two segments,one to detect errors and to throw exceptions and the other to catch exceptions and to take appropriate actions.

The basic concepts of exception handling are throwing an exception and catching.Java uses a keyword try to preface a block of code that is likely to cause an error conditions and "throw " an exception.

A catch block defined by the keyword catch "catches" the exception "thrown" by the try block and handles it appropriately.The catch block is added immediately after the try block.

program:

import java.io.*;

class file1

{

public static void main(string args[ ])

{

int b;

string str=" ";

datainputstreamobj=new datainputstream(system.in);

try

{

filewriter ins=new filewriter("input.txt");

system.out.println("Enter the input to the first file");

str=obj.readLine();

for(int i=0;I<str.length();i++)

ins.write(str.charAt(i)+10);

ins.close();

fileReaderinp=new FileReader("Input.txt");

fileWriter out=new FileWriter("Output.txt");

while((b=inp.read())!=-1)

out.write(b);

inp.close();

out.close();

fileReaderots=new FileReader("output.txt");

system.out.println("The content of the second file is");

while((b=ots.read())!=-1)

system.out.print((char)(b-10));

ots.close();

}

catch(IOException e)

{

system.out.println(e);

system.exit(-1);

}

}

pgm 2:

import java.io.*;

class ssort

{

public static void main (string args[])

{

int i=0;j=0;

int n=0;

string str[ ];

datainputstream ob=new datainputstream(system.in);

system.out.println("\n\t string sorting");

system.out.println("\t.................");

try

{

system.out.print("enter the number of string")

n=integer.paseint(ob.readline());

str=new string[n];

system.out.println("\n enter the strings:");

for(i=0;I<n;i++)

{

str[i]=ob.readline();

}

string tmp;

for(i=0;I<n-1 ;i++)

{

for(j=i+1;j<n;j++)

{

if(str[i].compareTo(str[j])>0)

{

tmp=str[i];

str[i]=str[j];

str[j]=tmp;

}

}

}

system.out.println("\n the encoded elements are:");

for(i=0; i<n; i++)

{

system.out.println(str[i]);

}

}

catch(Exception e)

{

}

}

}

Java supports another statement known as finally statement that can be used to handle an exception that is not caught by any of the previous catch statement finally block can be used to handle any exception generated with a try block.


Related Solutions

(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to...
(Code in Java please) You need to implement the GarageDoor, GarageDoorUpCommand and GarageDoorDownCommand classes (similar to Light, LightOnCommand and LightOffCommand), AND (edited) Implement the CeilingFan class (with state, see p. 220), AND CeilingFanHighCommand (p. 221), CeilingFanMediumCommand, CeilingFanLowCommand, CeilingFanOffCommand (WITH Undo), AND TEST your CeilingFan classes (see pp. 222 and 223), AND Finally, implement and Test the MacroCommand class on pp. 224-227 EXAMPLE output for homework assignment…………… ======= The COMMAND PATTERN ============= guest house garage door is UP guest house garage...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
This question has been answered before. I need a new, slightly modified code for the following:...
This question has been answered before. I need a new, slightly modified code for the following: A palindrome is a word that it reads the same left to right and right to left. For this programming assignment, you need to write a C++ program that does the following: Request the user to enter a string. Write a recursive function that will test the string to validate if it is a palindrome or not. Respond back with an output saying if...
Write java programs to implement CBC mode of data encryption standard?
Write java programs to implement CBC mode of data encryption standard?
a java code In this lab you will implement an inorder traversal, and a search for...
a java code In this lab you will implement an inorder traversal, and a search for a 2-3-4 tree. The inorder traversal will print the contents (integers) of the tree, and the search method will return a boolean, indicating whether a given key was found in the tree. Examine the provided template. The main method is provided for you, as well as a template of the Node and 2-3-4 classes. The main method will read either "inorder" or an integer...
AES128 encryption given by the following setting (Note: plaintext, Cipherkey and Ciphertext are Bytes) a. Plaintext:...
AES128 encryption given by the following setting (Note: plaintext, Cipherkey and Ciphertext are Bytes) a. Plaintext: 00……00 Cipherkey: 00……00 Ciphertext: 66 E9 4B D4 EF 8A 2C 3B 88 4C FA 59 CA 34 2B 2E b. Plaintext: 00……01 Cipherkey: 00……00 Ciphertext: 58 E2 FC CE FA 7E 30 61 36 7F 1D 57 A4 E7 45 5A c. Plaintext: 00……00 Cipherkey: 00……01 Ciphertext: 05 45 AA D5 6D A2 A9 7C 36 63 D1 43 2A 3D 1C 84...
Java question - How can the code below be modified to accept multi digit input. String...
Java question - How can the code below be modified to accept multi digit input. String e = "1+9+8"; int r = e.charAt(0)-'0'; for (int i = 1; i < e.length(); i+=2){    if (e.charAt(i) == '+'){ r += e.charAt(i+1)-'0'; } else{ r -= e.charAt(i+1)-'0'; } The only built in String methods that can be used are lowercase(), length(), and charAt(). Arrays and parseInt() cannot be used. So we want to know how we can get an answer if a...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
I need the Java Code and Flowchart for the following program: Suppose you are given a...
I need the Java Code and Flowchart for the following program: Suppose you are given a 6-by-6 matrix filled with 0s and 1s. All rows and all columns have an even number of 1s. Let the user flip one cell (i.e., flip from 1 to 0 or from 0 to 1) and write a program to find which cell was flipped. Your program should prompt the user to enter a 6-by-6 array with 0s and 1s and find the first...
Please compute AES128 encryption given by the following setting: (Note: Plaintext, Cipherkey and Ciphertext are Bytes)...
Please compute AES128 encryption given by the following setting: (Note: Plaintext, Cipherkey and Ciphertext are Bytes) Plaintext: 00……00 Cipherkey: 00……01 What is the ciphertext?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT