Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done - encrypt the message and decrypt the encoded message.

One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the alphabet are shifted by three, wrapping around at the end of the alphabet. For example,

PlainTest: a b c d e f g h i j k l m n o p q r s t u v w x y z
Caesar shift: d e f g h I j k l m n o p q r s t u v w x y z a b c

When encrypting a message, you take each lette r of the message and replace it with its corresponding letter from the shifted alphabet. To decrypt an encoded message, you simply reverse the operation. That is, you take the letter from the shifted alphabet and replace it with the corresponding letter from the plaintext alphabet. Thus the string the quick brown fox becomes wkh txlfn eurzq ira

Another type of cipher is known as Transposition cipher. In this type of cipher, letters in the original message are re-arranged in some methodical way - for instance, reverse the letters in each string. Thus the string the quick brown fox becomes eht kciuq nworb xof

Still yet another cipher method is the Reverser cipher. This method does not only reverses the letters in each word, but as does the Transposition cipher, but it also reverses the result generated from the Transposition cipher. Hence the original message the quick brown fox becomes xof nworb kciuq eht

Class design

Here are three Crypt ography methods - Caesar, Transposition and Reverser. They all have something in common. They encrypt and decrypt messages. That is, they take a string of words and translate each word using the encoding algorithm appropriate for that cipher. Thus each class cipher will need polymorphic encode() and decode() methods, which take a word and encodes and decodes it according to the rule of the particular cipher.

From a design perspective, the encrypt() method and the decrypt() methods will be the same for every class. They simply break message into words and have each word encode or decode. However, the encode and decode methods will be different for each cipher.

From the above analysis a partial abstract class Cipher is depicted be by Listing 1.

import java.uti l.StringTokenizer;

public abstract class Cipher
{
private String message; // The message string
StringBuffer encrypted_message,
decrypted_message;

public Cipher(String text)
{
// Complete definition
}

public final void encrypt()
{
/* The message string is tokenized into individual words, and each word is encoded
by calling the encode method
*/
encrypted_message = new StringBuffer("");
StringTokenizer words = new StringTokenizer(message);

while(words.hasMoreTokens())
{
String s = words.nextToken();
s = encode(s) + " ";
encrypted_message.append(s);
}
}

public final void decrypt(String message)
{
/* The encoded message string is tokenized into individual words, and each word is
encoded by calling the decode method
*/

decrypted_message = new StringBuffer("");
StringTokenizer words = new StringTok enizer(message);

while(words.hasMoreTokens())
{
String s = words.nextToken();
s = decode(s) + " ";
decrypted_message.append(s);
}
}

String getEncodedMessage()
{
return encrypted_message.toString();
}

String getDecodedMessage()
{
return decrypted_message.toString();
}

public abstract String encode(String s);
public abstract String decode(String s);
}

Listing 1. Abstract class Cipher

The class Caesar inherits the abstract class Cipher. This class defines the methods code and decode. The method encode takes a String parameter and returns a String result. It takes each character of the parameter and performs a Caesar shift on the character. That is, a shift with possible wrap around can be coded as follows:
char ch = word.charAt(i);
ch = (char)('a' + ch - 'a' + 3) % 26);

The method decode does the reverse. Listing 2 shows a partial definition of this class - Caesar

public class Caeser extends Cipher
{
public Caeser(String s)
{
super(s);
}
public String encode(String word)
{
StringBuffer result = new StringBuffer();

for (int i = 0; i < word.length(); i++)
{
char ch = word.charAt(i);
ch = determineCharacter(ch, Constants. ENCODE_SHIFT);
result.append(ch);
}
return result.toString();
}

public String decode(String word)
{
// Complete the method and return the decoded word
}

public char determineCharacter(char ch, int shift)
{
if(Character.isLowerCase(ch))
ch = (char)(''a'' + (ch - ''a'' + shift) % Constants.WRAP_AROUND);
return ch;
}
}

Listing 2.

In similar fashion, the class Transpose inherits Cipher and defines the methods code and encode. Listing 3 shows an incomplete definition of the class Transpose.

public class Transpose extends Cipher
{
Transpose(String s)
{
super(s);
}
public String encode(String word)
{
//Complete the method and return the encoded word
}
public String decode(String word)
{
// Complete the method and return the decoded word
}
}
Listing 3

The class Reverser inherits the class Transpose. Listing 4 shows an incomplete definition of this class.

public class Reverser extends Transpose
{
Reverser(String s)
{
super(s);
}

String reverseText(String word)
{
// Complete the method and returns the reverse transposed version;
}
}

Listing 4

Things to do
From the above discussion

. Complete the classes Cipher, Caesar, and Transpose, and Reverser. In addition
. Define an interface called Constants that will store the value 26 in the identifier WRAP_AROUND; similarly, store 3 in the identifier, ENCODE_SHIFT, and 23 in the identifier DECODE_SHIFT (needed to decode the encoded message). The code in the method determineCharacter(char ch, int shift) accounts for lower case letters only. Extend the code to include both upper and lower case letters.

Listing 5 Use the following test class to implement your other classes.

import javax.swing.JOptionPane;

class TestEncryption
{
public static void main(String arg[])
{
String code, output = "";

String text = JOptionPane.showInputDialog("Enter message");

output += "The original message is \n" + text + "\n";

Cipher c = new Caeser(text);
c.encrypt();
code = c.getEncodedMessage();
output += "\nCeasar Cipher\nThe encrypted message is \n" + code + "\n";
c.decrypt(code);
code = c.getDecodedMessage();
output +="The decrypted message is \n" + code + "\n";

c = new Transpose(text);
c.encrypt( );
code = c.getEncodedMessage();
output += "\nTranspose\nThe encrypted Transpose message is \n" + code + "\n";
c.decrypt(code);
code = c.getDecodedMessage();
output +="The decripted Transpose message is \n" + code + "\n";

Reverser r = new Reverser(text);
r.encrypt();
code = r.reverse( r.getEncodedMessage() );
output += "\nReverser\nThe encrypted Reverse message is \n" + code+ "\n";
code = r.decode(code);
output+="The decrypted Reverse message is \n" + code;

System.out.println(output);
}
}

Java, Programming

  • Category:- Java
  • Reference No.:- M91409482
  • Price:- $100

Guranteed 48 Hours Delivery, In Price:- $100

Have any Question?


Related Questions in Java

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Can someone kindly help me to consider whether java

Can someone kindly help me to consider whether Java provides the facility of operator overloading? If it does, may you kindly describe how overloading operators can be accomplished? If not, may you kindly describe why yo ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As