Question

In: Computer Science

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 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 “csimuylangpwzfrxbvhdtejqko”.

***Java language***

Solutions

Expert Solution

/********* EncryptDecrypt.java ***********/

import java.util.Scanner;

public class EncryptDecrypt {

   public static void main(String[] args) {

       String mesg = "csimuylangpwzfrxbvhdtejqko";
       String encrypt = "", decrypt = "", line;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       int choice;
       /*
       * This while loop continues to execute until the user enters a valid
       * choice or 3
       */
       while (true) {
           // displaying the menu
           System.out.println("You may:");
           System.out.println("\t1) Encrypt a message");
           System.out.println("\t2) Decrypt a message");
           System.out.println("\t3) Quit");

           // getting the choice entered by the user
           System.out.print("Enter choice (1,2, or 3): ");
           choice = sc.nextInt();

           // Based on the user choice the corresponding case will be executed
           switch (choice) {
           case 1: {
               sc.nextLine();
               // Getting the input entered by the user
               System.out.print("Enter the message :");
               line = sc.nextLine();
               line = line.toLowerCase();

               // Encrypting the message
               for (int i = 0; i < line.length(); i++) {
                   if (line.charAt(i) >= 'a' && line.charAt(i) <= 'z') {
                       int val = (int) (line.charAt(i));
                       encrypt += mesg.charAt((val - 97));
                   } else {
                       encrypt += line.charAt(i);
                   }

               }
               System.out.println("Your encrypted message is:");
               System.out.println(encrypt);

               continue;
           }
           case 2: {
               sc.nextLine();
               System.out.print("Enter the message :");
               line = sc.nextLine();
               String lowerline = line.toLowerCase();

               // Decrypting the message
               for (int i = 0; i < lowerline.length(); i++) {
                   if (lowerline.charAt(i) >= 'a'
                           && lowerline.charAt(i) <= 'z') {
                       for (int j = 0; j < mesg.length(); j++) {
                           if (mesg.charAt(j) == lowerline.charAt(i)) {
                               if (Character.isUpperCase(line.charAt(i)))
                                   decrypt += (char) (65 + j);
                               else
                                   decrypt += (char) (97 + j);
                               break;
                           }
                       }
                   } else {
                       decrypt += line.charAt(i);
                   }

               }
               System.out.println("Your decrypted message is: ");
               System.out.println(decrypt);

               continue;
           }
           case 3: {
               break;
           }

           default: {
               continue;
           }

           }
           break;
       }

   }

}

/**********************************************/

/**********************************************/

Output:

/**********************************************/


Related Solutions

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...
Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".
Create a Columnar Transposition Cipher in Python that encrypts/decrypts the word "Crypt".
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...
COP2271 MATLAB HW9 Homework: Modified Vigenere Cipher Implement a decryption cipher to decode messages using a...
COP2271 MATLAB HW9 Homework: Modified Vigenere Cipher Implement a decryption cipher to decode messages using a secret key. You are required to submit the solution and screenshots for this question. Key programming concepts: if statements, loops, strings Approximate lines of code: 27 (does not include comments or white space) Commands you can’t use: None... Program Inputs • Enter message to decrypt: • Enter secret key: – The user will always enter text for all prompts, no error checking needed. The...
In python make a simple code. You are writing a code for a program that converts...
In python make a simple code. You are writing a code for a program that converts Celsius and Fahrenheit degrees together. The program should first ask the user in which unit they are entering the temperature degree (c or C for Celcius, and f or F for Fahrenheit). Then it should ask for the temperature and call the proper function to do the conversion and display the result in another unit. It should display the result with a proper message....
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...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The program will now be broken up into methods and store all the inventory in an ArrayList object. The program will be able to run a report for all inventory details as well as a report for items that are low in...
Problem statement: You are tasked with writing a simple program that will keep track of items...
Problem statement: You are tasked with writing a simple program that will keep track of items sold by a retail store. We need to keep track of the stock (or number of specific products available for sale). Requirements: The Food and Book items should inherit all the properties of the Product item in the previous assignment. Foods cannot be added to the inventory without an expiration date. Implement a toString method for Product, Food, and Book. Grading details: Correct usage...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT