In: Computer Science
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:
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
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;
}
}