Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the classes described below to perform two types of encryption using an interface class. a. Write a Cryptography interface class with the following features.

Write the classes described below to perform two types of encryption using an interface class.

a. Write a Cryptography interface class with the following features. Note that it will not include actual method definitions.

An encrypt method receives a String to be encrypted and returns the encrypted String.

A decrypt method receives a String to be decrypted and returns the decrypted String.

A printCodes method receives a String and returns nothing.

b. Write a Caesar class that implements Cryptography and has the following features. See the program CaesarExample.java that implements the algorithm.

The private instance variable (integer) shift is the amount to be added to a character for encryption.

The constructor receives a value for the shift value.

An encrypt method receives a String to be encrypted and returns a String encrypted by adding the shift value to each original character value.

A decrypt method receives a String to be decrypted and returns a String decrypted by subtracting the shift value from each encrypted character value.

A printCodes method receives a String and prints the ASCII codes of each character. To print the numerical code of a char, convert it to (int) before printing. Though not required, it is recommended that you print the numbers horizontally for visibility.

c. Write a class that implements Cryptography and uses any algorithm you wish. (You may design your own or research other algorithms.) It should have the following features.

A comment explaining the algorithm, including an example. If you use an algorithm you found, provide a URL or other reference to cite a source for the algorithm.

At least two private instance variables (any type) that are used in the algorithm.

The constructor receives values for the instance variables.

Methods encrypt and decrypt each receive a String to be encrypted or decrypted, respectively, and return the resulting String using the algorithm you chose or designed.

A printCodes method works exactly as in part (b).

d. Write an application class Problem2.java that creates uses either the Caesar class or your custom class as described below.

Ask the user to enter: (1) whether they wish to encrypt or decrypt, and (2) which algorithm to use.

Print the ASCII codes of the entered text using the printCodes method. (This is intended to allow you to verify that the algorithm is working correctly.)

Print the resulting String using either the encrypt or decrypt method of the proper algorithm.

Below are examples of possible output (user input in bold). Note that the Caesar shift used was 3.

Example #1:

Encrypt or decrypt? encrypt

Caesar or custom? caesar

Enter message: Hello. This is a test!

The character codes are: 72 101 108 108 111 46 32 84 104 105 115 32 105 115

32 97 32 116 101 115 116 33

The encrypted message is: Khoor1#Wklv#lv#d#whvw$

Example #2

Encrypt or decrypt? decrypt

Caesar or custom? caesar

Enter message: Khoor1#Wklv#lv#d#whvw$

The character codes are: 75 104 111 111 114 49 35 87 107 108 118 35 108 118

35 100 35 119 104 118 119 36

The decrypted message is: Hello. This is a test!

CaesarExample.java

public class CaesarExample {

public static void main(String[] args) {

int shift = 3;

// Encryption

String mString = "Hello. This is a test!"; // Original message

String cString = ""; // Cypher (encrypted string)

for (int iLetter = 0; iLetter < mString.length(); iLetter++) {

int m = mString.charAt(iLetter); // Message character

cString += (char) (m + shift);

}

System.out.println("Encrypted: " + cString);

// Decryption

String rString = ""; // Recovered string

for (int iLetter = 0; iLetter < cString.length(); iLetter++) {

int c = cString.charAt(iLetter); // character

rString += (char) (c - shift);

}

System.out.println("Decrypted: " + rString);

}

}

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

c. How is trust demonstrated?

Answered: 1 week ago

Question

Have issues been prioritized?

Answered: 1 week ago

Question

d. How will lack of trust be handled?

Answered: 1 week ago