Question

In: Computer Science

Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...

Programing Language: Java

The Problem:
You are writing a program that encrypts or decrypts messages using a simple substitution
cipher. Your program will use two constant strings. One will represent the code for
encryption: going from the original message (called the plaintext) to the encrypted
version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase
alphabet.
Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a
message, or 3) quit.
If they choose encrypt or decrypt, you will then get the message from the user and change
it to all lower case. Then you will print out the encrypted or decrypted message character
by character, using the information in the code to be used for encryption or decryption.
The String you will use for encryption is “bopcardxshewityjlqfguvzmkn”.
How the substitution code works:
With a substitution encryption, we select a substitute letter for each letter. Our encryption
String has the substitute for ‘a’ at the first index (0) and the substitute for ‘b’ at the next
index (1) and so on up to the substitute for ‘z’ at the last index (25). So what we will do is
to look at the letter we want to encrypt, determine the index that corresponds to that letter
using our alphabet string (and indexOf()) and then get the character at that index from the
encryption code.
Decryption will be similar except that we will get the index of the letter in the encryption
string and then get the character at that index from the alphabet string.
We will only substitute letters. All other characters will get passed on as is.
Sample Program Run (user input in bold):
Welcome to the Normal Encrypter!
You may:
1) Encrypt a message
1
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 1
Enter the message: Hello, world!
Your encrypted message is:
auwwr, jrvwm!
You may:
1) Encrypt a message
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 2
Enter the message: Auwwr, jrvwm!
Your decrypted message is:
Hello, world!
You may:
1) Encrypt a message
2) Decrypt a message
3) Quit
Enter choice (1, 2, or 3): 3
Thank you for using the Normal Encrypter!

Solutions

Expert Solution

Code:

import java.util.*;
public class Main
{
   public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int n;
   String plaintext= "abcdefghijklmnopqrstuvwxyz";
   String encrypt="bopcardxshewityjlqfguvzmkn";
   System.out.print("Welcome to the Normal Encrypter!\nYou may:\n1) Encrypt a message\n2) Decrypt a message\n3) Quit\nEnter choice (1, 2 or 3):");
   n=Integer.parseInt(sc.nextLine());
   while(n!=3)
   {
   if(n == 1 || n == 2)
   {
   System.out.print("Enter the message:");
   String s=sc.nextLine();
   int l=s.length();
   String text="";
   if(n == 1)
   {
   s=s.toLowerCase();
   for(int i = 0;i < l; i++)
   {
   char p=s.charAt(i);
   if(Character.isLetter(p))
   text = text + encrypt.charAt(plaintext.indexOf(p));
   else
   text=text+p;
   }
   System.out.println("Your decrypted message is:\n"+text);
   }
   else if(n == 2)
   {
   for(int i = 0;i < l; i++)
   {
   char p=s.charAt(i);
   if(Character.isLetter(p))
   {
   if(Character.isUpperCase(p))
   text = text + Character.toUpperCase(plaintext.charAt(encrypt.indexOf(Character.toLowerCase(p))));
   else
   text = text + plaintext.charAt(encrypt.indexOf(p));
   }
   else
   text=text+p;
   }
   System.out.println("Your encrypted message is:\n"+text);
   }
   }
   else
   System.out.println("Invalid choice");
   System.out.print("You may:\n1) Encrypt a message\n2) Decrypt a message\n3) Quit\nEnter choice (1, 2 or 3):");
   n=Integer.parseInt(sc.nextLine());
  
   }
   System.out.println("Thank you for using the Normal Encrypter!");
   }
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

Here the encryption key given in the question and the encryption key used for Sample Program Run are not same, so the answer varies.

For any doubt or question comment below.


Related Solutions

Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
CODE IN PYHTON Write a program which DECRYPTS the secret messages which are created by the...
CODE IN PYHTON Write a program which DECRYPTS the secret messages which are created by the ENCRYPT program which we went over in class. It should first prompt the user for the scrambled alphabet which was created by ENCRYPT (which you should be able to copy & paste from the preceeding program's run). It then should ask for the secret message. Finally, it outputs the unscrambled version. Note that there are exactly 26 characters input for the scrambled alphabet. All...
***USING JAVA Scenario: You will be writing a program that will allow a user to find...
***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word. Requirements: Do not use Arrays for this assignment. Do not use any String class methods (.phrase(), replace methods, regex methods)...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
programing language JAVA: Design and implement an application that reads a sentence from the user, then...
programing language JAVA: Design and implement an application that reads a sentence from the user, then counts all the vowels(a, e, i, o, u) in the entire sentence, and prints the number of vowels in the sentence. vowels may be upercase
in C++ programing language Write a program that prompts the user for an integer, then prints...
in C++ programing language Write a program that prompts the user for an integer, then prints all of the numbers from one to that integer, separated by spaces. Use a loop to print the numbers. But for multiples of three, print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz". Drop to a new line after printing each 20 numbers. If the user typed...
c programing language When a new program is executed, a new process is created with the...
c programing language When a new program is executed, a new process is created with the next available process ID. However, there are a few special processes that always have the same process ID, which are usually given the ID value less than 5 these are called system processes. Can you identify which of the two system processes have the process ID of 0 and 1 respectively?
This program is to be written in Java Language. Thank you A College has conducted a...
This program is to be written in Java Language. Thank you A College has conducted a student survey. Students were asked to rate their satisfaction with remote learning courses. Students rated their satisfaction on a scale of 1 to 5 (1 = "I hate it", 5 = "I love it"). The student responses have been recorded in a file called "StudentResponses.txt". Each line of the file contains one student response. Program 1 You are to write a program that reads...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT