Question

Security is an important feature of information systems. Often, text is encrypted before being sent, and...

Security is an important feature of information systems. Often, text is encrypted before being sent, and then decrypted upon receipt. We want to build a class (or several classes) encapsulating the concept of encryption. You will need to test that class with a client program where the main method is located.

For this project, encrypting consists of translating each character into another character. For instance, if we consider the English alphabet, including characters a through z, each character is randomly encrypted into another, which could be the same character. (If you like, you can design your program so that no character is encrypted into itself.) To represent this concept, we can have an array of characters for the original alphabet, and another array of characters for the encrypted alphabet.

To encrypt a word, each letter in the word is replaced by the corresponding letter in the encryted alphabet. For example, the word caged would be encrypted into huzsa. To decrypt a word, the letters in the encrypted word are replaced by the corresponding letter in the original alphabet. For example, the encrypted word xssa would be decrypted as feed.

If we have 26 different characters in the original alphabet, then we will have 26 different characters in the encrypted alphabet. Furthermore, the encrypted alphabet should be randomly generated.

In your main method, you should prompt the user for a sentence. Your program should encrypt the sentence, output the encrypted sentence, then decrypt it, and output the decrypted sentence, which should be identical to the original sentence that was input by the user.

-------------------------------------------------------------------------------------------------------

To implement this program, you will have two classes: Cryptogram and Client.

Cryptogram class:

data members:

alphabet array: This is a character array that stores the letters of the alphabet- hard code the values

cryptCode array: This is a character array that stores the code of the replacement letters- just declare it. We will fill it with values in the constructor

Methods

1. Constructor

a. Set the size of the cryptCode array to the same size as alphabet array

b. Copy the contents of alphabet array into cryptCode array

c. Shuffle the letters in the array by moving backward in the array and randomly picking a letter and placing the letter at the end of the subarray

2. toString: Returns the alphabet and the cryptoCode with appropriate labels

3. findCode: Receives the character to encode and returns the index value of the substitution character or -1 if the parameter is not a letter. This method searches the alphabet array for the location of the character received and returns the location that the value was found in the array.

4. encrypt: Receives the phrase to encrypt and returns the encrypted phrase. This method will repeatedly call the findCode method for each letter of the phrase received until all letters of the phrase have been converted. Each time it calls the findCode, it will add to the encrypted phrase.

5. findLetter: Receives the character to decode and returns the index of the substitution character or -1 if the parameter is not a letter. This method searches the cryptoCode array for the location of the character received and returns the location in which it was found

6. decrypt: Receives the encrypted phrase and returns the decrypted phrase. This method will repeatedly call the findLetter method for each letter of the encrypted phrase until all letters of the phrase have been converted back to the original message.

CryptogramClient Class

1. Instantiate Cryptogram

2. Output the toString method of Cryptogram

3. Prompt and accept a phrase to encrypt.

4. Output the encrypted phrase.

5. Output the decrypted phrase.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Cryptogram class:

import java.util.Arrays;
import java.util.Random;

public class Cryptogram {
  
char[] alphabet = {'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'};
char[] cryptCode ;
  
//Constructor
Cryptogram()
{
//Set the size of the cryptCode array to the same size as alphabet array
cryptCode = new char[26];
Random r = new Random();
// Copy the contents of alphabet array into cryptCode array
for (int i = 0; i <26; i++)
cryptCode[i] = alphabet[i];

/*Shuffle the letters in the array by moving backward in the array and randomly picking a letter and placing the letter at the end of the subarray */
for (int i = 26-1; i > 0; i--) {

// Pick a random index from 0 to i
int j = r.nextInt(i);

// Swap cryptCode[i] with the element at random index
char temp = cryptCode[i];
cryptCode[i] = cryptCode[j];
cryptCode[j] = temp;
}

  
}
// toString: Returns the alphabet and the cryptoCode with appropriate labels
public String toString()
{
return "Alphabet: " + Arrays.toString(alphabet) + "\nCryptCode: " + Arrays.toString(cryptCode);
}
/*Receives the character to encode and returns the index value of the substitution character or -1 if the parameter is not a letter. This method searches the alphabet array for the location of the character received and returns the location that the value was found in the array. */
public int findCode(char c)
{
for (int i = 0; i <26; i++)
if(c == alphabet[i])
return i;

return -1;
}
/*Receives the phrase to encrypt and returns the encrypted phrase. This method will repeatedly call the findCode method for each letter of the phrase received until all letters of the phrase have been converted. Each time it calls the findCode, it will add to the encrypted phrase. */
public String encrypt(String phrase)
{
String enc_phrase = "";
for (int i = 0; i <phrase.length(); i++)
{   
int loc = findCode(phrase.charAt(i));
if(loc == -1)
enc_phrase += phrase.charAt(i);
else
enc_phrase += cryptCode[loc];
}
return enc_phrase;
}
/* Receives the character to decode and returns the index of the substitution character or -1 if the parameter is not a letter. This method searches the cryptoCode array for the location of the character received and returns the location in which it was found */
public int findLetter(char c)
{
for (int i = 0; i <26; i++)
if(c == cryptCode[i])
return i;

return -1;
}
/* Receives the encrypted phrase and returns the decrypted phrase. This method will repeatedly call the findLetter method for each letter of the encrypted phrase until all letters of the phrase have been converted back to the original message. */
public String decrypt(String enc_phrase)
{
String dec_phrase = "";
for (int i = 0; i <enc_phrase.length(); i++)
{  
int loc = findLetter(enc_phrase.charAt(i));
if(loc == -1)
dec_phrase += enc_phrase.charAt(i);
else
dec_phrase += alphabet[loc];
}
return dec_phrase;
}
}

CryptogramClient Class:

import java.util.Scanner;

public class CryptogramClient {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Instantiate Cryptogram
Cryptogram c = new Cryptogram();
//Output the toString method of Cryptogram
System.out.println(c.toString());
//Prompt and accept a phrase to encrypt.
System.out.println(" Enter a senetence: ");
String phrase = sc.nextLine();
String enc = c.encrypt(phrase);
// Output the encrypted phrase.
System.out.println("Encrypted text: " + enc);
String dec = c.decrypt(enc);
//Output the decrypted phrase.
System.out.println("Decrypted text: " + dec);
}
  

OUTPUT:

Alphabet: [a, b, c, d, e, f, g, h, i, j, k, 1, m, n, o, p, q, r, s, t, u, v, w, x, y, z] CryptCode: [W, P, x, g, q. j. c, k, 1. b, u, x, n, v, e, i, m, y, h, s, a, z, f, o, d, t] Enter a senetence: hi how r u! Encrypted text: kl kef y a!

Add a comment
Know the answer?
Add Answer to:
Security is an important feature of information systems. Often, text is encrypted before being sent, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information...

    JAVA Problem: With the recent news about data breaches and different organizations having their clients’ information being exposed, it is becoming more and more important to find ways to protect our sensitive data. In this program we will develop a simple tool that helps users generate strong passwords, encrypt and decrypt data using some cyphering techniques. You will need to create two classes. The first class is your driver for the application and contains the main method. Name this class...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    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...

  • Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...

    Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher, to encrypt and decrypt a message. Your encryption key and message are to be read in from two different files and then the encrypted message will be output to third file. Then the encrypted message is read in and decrypted to a fourth file. A monoalphabetic cipher starts with an encryption word, removes the redundant letters from the word, and assigns what’s left to...

  • C++ Code

    "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh.The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The convention in cryptography is to write the plain text in lower case letters and the encrypted text in upper case...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • ****************************************************************************************************************8 I want it same as the output and with details please and comments "For two...

    ****************************************************************************************************************8 I want it same as the output and with details please and comments "For two thousand years, codemakers have fought to preserve secrets while codebreakers have tried their best to reveal them." - taken from Code Book, The Evolution of Secrecy from Mary, Queen of Scots to Quantum Cryptography by Simon Singh. The idea for this machine problem came from this book.You will encrypt and decrypt some messages using a simplified version of a code in the book. The...

  • Write the programming C please, not C++. The main function should be to find the offset...

    Write the programming C please, not C++. The main function should be to find the offset value of the ciper text "wPL2KLK9PWWZ7K3ST24KZYKfPMKJ4SKLYOKRP4KFKP842LK0ZTY43 " and decrypt it. In cryptography, a Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • Please help me write this Java program. I had posted this question before, but got an...

    Please help me write this Java program. I had posted this question before, but got an answer that was totally wrong. We are using the latest version of Java8. Thank You! -------------------------------------------------------------------------------------- Write a Java program that can successfully DECRYPT a string inputted by the user which has been encrypted using a Caesar Cipher with a unknown shift value(key). You can use brute force to do a for loop through all the 26 shift values, however, your program should only...

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT