In: Computer Science
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.
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.