Question

In: Computer Science

Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the...

Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key.

Use the following cipher:

  • Take the key and mod it by 26. Example: a key of 30 becomes 4.
  • If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary.
  • If the character is not a letter, then shift it by the key but do not wrap.

Check the test cases for example.

Make getters to support the CipherDemo. Also, make two custom Exceptions called UselessKeyException and EmptyPlainText. In your constructor, throw UselessKeyException if the key is divisible by 26 and throw EmplyPainText if the plain text is zero characters.

CipherDemo.java :

import java.util.Scanner;

public class CipherDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter some text to encrypt");
        String input = keyboard.nextLine();
        System.out.println("Enter a key");
        int key = keyboard.nextInt();

        try {
            Cipher c = new Cipher(input, key);
            System.out.println("Plain text: " + c.getPlainText());
            System.out.println("Cipher text: " + c.getCipherText());
            System.out.println("Key: " + c.getKey());
        } catch (EmptyPlainText e) {
            System.out.println(e.getMessage());
        } catch (UselessKeyException e) {
            System.out.println(e.getMessage());
            System.out.println("Useless key: " + e.getUselessKey());
        }
    }
}

input

abcENTER
1ENTER

output:

Enter some text to encrypt\n
Enter a key\n
Plain text: abc\n
Cipher text: bcd\n
Key: 1\n

input:

Hello, my secret password is SIMCITY! Don't tell anyone! I've used this for 400 days.ENTER
23

output:

Enter some text to encrypt\n
Enter a key\n
Plain text: Hello, my secret password is SIMCITY! Don't tell anyone! I've used this for 400 days.\n
Cipher text: EbiilC7jv7pbzobq7mxpptloa7fp7PFJZFQV87Alk>q7qbii7xkvlkb87F>sb7rpba7qefp7clo7KGG7axvpE\n
Key: 23\n

input:

With computer science, you can work in any industry.ENTER
5021ENTER

output:

Enter some text to encrypt\n
Enter a key\n
Plain text: With computer science, you can work in any industry.\n
Cipher text: Zlwk#frpsxwhu#vflhqfh/#brx#fdq#zrun#lq#dqb#lqgxvwub1\n
Key: 5021\n

input:

With computer science, you can work in any industry.ENTER
26ENTER

output:

Enter some text to encrypt\n
Enter a key\n
Error: Key is divisible by 26. That's a bad key!\n
Useless key: 26\n

input : 23

output:

Enter some text to encrypt\n
Enter a key\n
Error: Nothing to encrypt!\n

Solutions

Expert Solution

Given below is the code for question. Run the CipherDemo provided by instructor. Please do rate the answer if it helped. Thank you.

EmptyPlainText.java
---

public class EmptyPlainText extends Exception {

   public EmptyPlainText() {
       super();
   }

   public EmptyPlainText(String message) {
       super(message);
   }
  
}

UselessKeyException.java
-------

public class UselessKeyException extends Exception {
   private int uselessKey;
   public UselessKeyException(int key, String msg) {
       super(msg);
       uselessKey = key;
   }
   public int getUselessKey() {
       return uselessKey;
   }
  
  
}

Cipher.java
---

public class Cipher {
   private String plainText;
   private int key;
  
   public Cipher(String text, int key) throws EmptyPlainText, UselessKeyException {
       if(text == null || text.length() == 0)
           throw new EmptyPlainText("Error: Nothing to encrypt!");
       if(key % 26 == 0)
           throw new UselessKeyException(26, "Error: Key is divisible by 26. That's a bad key!");
      
       this.plainText = text;
       this.key = key;
   }

   public String getPlainText() {
       return plainText;
   }

   public int getKey() {
       return key;
   }
  
   public String getCipherText() {
       String cipher = "";
       int k = key % 26;
       char c;
       int d;
      
       for(int i = 0; i < plainText.length(); i++) {
           c = plainText.charAt(i);
           if(Character.isUpperCase(c)) {
               d = c - 'A';
               d += k;
               d %= 26; //wrap around
               c =(char)( d + 'A');
           }
           else if(Character.isLowerCase(c)) {
               d = c - 'a';
               d += k;
               d %= 26; //wrap around
               c =(char) (d + 'a');
           }
           else
               c += k;
          
           cipher += c;
       }
       return cipher;
   }
  
  
  
}


Related Solutions

Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
1. Use a Vigenere cipher with a key of "Patton" to encrypt: "If everyone is thinking...
1. Use a Vigenere cipher with a key of "Patton" to encrypt: "If everyone is thinking alike, then somebody isn't thinking." Is this a good key? Why or why not? 2.  Calculate the index of coincidence for the result. SHOW ALL WORK PLEASE
Part 1: Design a Cipher allow user to use a “key” to encrypt and decrypt the...
Part 1: Design a Cipher allow user to use a “key” to encrypt and decrypt the message. Use at least two ciphers combined to design your own cipher Specify the min. and max. length of the message user can enter to encrypt Specify the length of the “key” user can enter to encrypt and decrypt the message Part 2: Programme the cipher and make it available to validate. Cleartext for the original programming scripts has to submitted. A frontend webpage...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the...
Programming Language C++ Encrypt a text file using Caesar Cipher. Perform the following operations: Read the console input and create a file. ['$' character denotes end of content in file.] Close the file after creation. Now encrypt the text file using Caesar Cipher (Use key value as 5). Display the contents of the updated file. #include <iostream> using namespace std; int main() { // write code here }
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT