The PolyAlphabeticCipher class implements the Cipher interfa

The PolyAlphabeticCipher class implements the Cipher interface as described below.

Our version of the cipher requires the conversion of plaintext to ciphertext with the help of a single key. For this assignment assume that the key is a string containing only the letters A-Z (uppercase). The key is applied repeatedly such that every character in the plaintext is paired with a character in the key. For example, if the plaintext is \"The enemy was spotted\" and the key is \"COFFEE\", then you need to apply the key as \"COFFEECOFFEECOFFEECOF\" because there are 21 characters in the plaintext. Note that the key is required four times, the last one being incomplete. Assume the value of combination of the key and the character will not exceed 127.

The code values for the characters in the plaintext are their Unicode values. You don\'t need to know these values. You can obtain the code for any char ch by casting ch to an int: (int)ch.

The code values of the characters in the key are not their Unicode values. Instead, they are: A=0, B=1, C=2, ... , Z=25. That is, if ch is a character in the key, then the code for that character ch is
((int)ch - (int)\'A\').

The code for every letter in the plaintext is added to the code of a key character to produce a character in the cipher text.

Here is an example using the plaintext \"The enemy was spotted\" and the key \"COFFEE\".

Decryption of a ciphertext is done by reversing the process. We subtract the code of the key characters from the code of characters in the ciphertext.

For this assignment assume that the plaintext to be encrypted can contain the lettersA-Z, a-z, and spaces (but no tabs or other whitespace). No punctuation characters will be used. The ciphertext used as an argument to the decrypt method can only contain characters that correspond to the substitutions of the characters specified for the plaintext.

Constructor:

public PolyAlphabeticCipher(String key)

The argument key is provided to the constructor. This key is used during encryption and decryption.

List of attributes:

private String key; // This stores the key used in the polyalphabetic substitution cipher.

List of methods:

Implement the two methods declared in the Cipher interface.

-public String encrypt(String plaintext);

-public String decrypt(String ciphertext);

Solution

public class PolyAlphabeticCipher { private String key; public PolyAlphabeticCipher(String key) { this.key = key; } public String encrypt(String plaintext) { plaintext = plaintext.toLowerCase(); int keyLength = key.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < plaintext.length(); i++) { char value = plaintext.charAt(i); char code = key.charAt(i%keyLength); char result = (char) ((int) value + (int) code - 65); sb.append(result); } return sb.toString(); } public String decrypt(String ciphertext) { ciphertext = ciphertext.toLowerCase(); int keyLength = key.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ciphertext.length(); i++) { char value = ciphertext.charAt(i); char code = key.charAt(i%keyLength); char result = (char) ((int) value - (int) code + 65); sb.append(result); } return sb.toString(); } }
The PolyAlphabeticCipher class implements the Cipher interface as described below. Our version of the cipher requires the conversion of plaintext to ciphertext

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site