Question

In: Computer Science

Modify the Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase...

  1. Modify the Encryption program so that it uses the following encryption algorithm:

    • Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A)

    • Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9 to 8, 8 to 7, … 1 to 0, 0 to 9)

    • Everything else unchanged.

Hand in:

Encryption.java

Note:

You must modify the specifications (API) of the methods involved as well.  

Solutions

Expert Solution

Thanks for the question, It would have good if you have shared the previous code that needed to be modified, nevertheless, I have written the method that takes in a plain text and encrypt into based on the logic asked in the quesiton.

Here is the code.

================================================================================

import java.util.Scanner;

public class Encryption {

    /**
     *  Main logic of encryption
     * @param plainText : The text we want to encrypt
     * @return : The encrypted text using the given logic
     */
    public static String encrypt(String plainText) {

        String encrypted = "";
        for (int i = 0; i < plainText.length(); i++) {

            char letter = plainText.charAt(i);
            // logic to handle lowercase letters
            if (Character.isLowerCase(letter)) {
                letter = (char) (letter + 1);
                if (Character.isLowerCase(letter)) {
                    encrypted += String.valueOf(letter);
                } else {
                    encrypted += String.valueOf('a');
                }
            }
            // logic to handle uppercase letters
            else if (Character.isUpperCase(letter)) {
                letter = (char) (letter + 1);
                if (Character.isUpperCase(letter)) {
                    encrypted += String.valueOf(letter);
                } else {
                    encrypted += String.valueOf('A');
                }
            } 
            // logic to handle digits character
            else if (Character.isDigit(letter)) {
                letter = (char) (letter - 1);
                if (Character.isDigit(letter)) {
                    encrypted += String.valueOf(letter);
                } else {
                    encrypted += String.valueOf('9');
                }
            }
            // logic to handle characters which are not alpha numeric
            else {
                encrypted += String.valueOf(letter);
            }
        }
        return encrypted; // returns the encrypted stirng
    }

    public static void main(String[] args) {


        Scanner scanner = new Scanner(System.in);
           // ask user to enter a text to encrypt
        System.out.print("Enter text you like to encrypt: ");
        String plainText = scanner.nextLine();
             // display the encrypted string
        System.out.println("Encrypted String: "+ encrypt(plainText));

    }
}

=====================================================================


Related Solutions

Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and...
Question The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java or Python code to implement either Monoalphabetic cipher or Hill cipher or Transposition Cipher (Encryption and Decryption) and test your code on given plaintext. User may enter value of key at the command prompt, if required.
The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”....
The given plaintext is “Feistel cipher structure uses the same algorithm for both encryption and decryption”. Write Java code to implement Shift cipher (Encryption and Decryption) and test your code on given plaintext. Your code must meet following conditions. 1. User must enter the value of key from command prompt and print it at command prompt. 2. Print the cipher text and the plaintext at the command prompt after encryption and decryption. 3. Test your algorithm for 5 different key...
Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase,...
C++ Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). Please use a Queue Class and Stack class.
What is the output of the following program? Slightly modify the program so that: [Pts. 10]...
What is the output of the following program? Slightly modify the program so that: [Pts. 10] The Parent process calculates the val such a way that its value is 20 and it prints “This is parent process and val = 20”. Also, slightly modify The child process calculates the val such a way that its value is 25 and it prints “This is child process and val = 25”. int main() { int val = 15; int pid; if (pid...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot...
in C++, Modify the quicksort algorithm such that it uses the last item as the pivot instead of the 1st. Also, sort in descending order, instead of ascending order. NOTE: Do not move the last element into the first element of the array. You must treat the algorithm as if the pivot is actually sitting in the last location of the array. After it has been sorted in descending order, go through all the items in the array and make...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method. Below is the source code for the driver class: public class EmployeeTest10A { public static void main(String[]...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT