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

You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your...
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...
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...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads...
A question about exceptions, the language is JAVA. The purpose is writing a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock , for example 23:47:55). Here are the input errors (Exceptions)...
***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...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT