Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please do just part C, part a and b done and I include them here with the origin

ID: 3794022 • Letter: P

Question

Please do just part C, part a and b done and I include them here with the original code. Test the result to make sure everything work fine and please type the code.

Original Code

Code was done for A and B

// CaesarCipher.java


public class CaesarCipher {

protected char[] encoder = new char[52];
protected char[] decoder = new char[52];
  
public CaesarCipher(int rotation) {
for(int k = 0; k < 26; k++) {
encoder[k] = (char)('A'+ (k+rotation)%26);
decoder[k] = (char)('A'+ (k-rotation + 26)%26);
}
for(int k = 0; k < 26; k++) {
encoder[k+26] = (char)('a'+ (k+rotation)%26);
decoder[k+26] = (char)('a'+ (k-rotation + 26)%26);
}
}
  
public String encrypt(String message)
{
return transform(message, encoder);
}
  
public String decrypt(String secret)
{
return transform(secret, decoder);
}
  
private String transform(String original, char[] code) {
char[] msg = original.toCharArray();
for(int k = 0; k < msg.length; k++){
if (Character.isUpperCase(msg[k])) {
int j = msg[k] - 'A';
msg[k] = code[j];
}
else
{
if (Character.isLowerCase(msg[k])) {
int j = msg[k] - 'a';
msg[k] = code[j+26];
}
}
}
return new String(msg);
}
  
public static void main(String[] args)
{
CaesarCipher cipher = new CaesarCipher(1);
System.out.println("Encryption code = " + new String(cipher.encoder));
System.out.println("Decryption code = " + new String(cipher.decoder));
  
String message = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String coded = cipher.encrypt(message);
System.out.println("Secret: " + coded);
String answer = cipher.decrypt(coded);
System.out.println("Message: " + answer);
}
  
}

// SubstitutionCipher.java


public class SubstitutionCipher {
  
CaesarCipher cc = null;
  
public SubstitutionCipher(String encoder)
{
int rotation = ('A' + encoder.charAt(0) -26)%26;
System.out.println(rotation);
cc = new CaesarCipher(rotation);
}
  
public String encrypt(String message)
{
return cc.encrypt(message);
}
  
public String decrypt(String secret)
{
return cc.decrypt(secret);
}
  
public static void main(String[] args)
{
SubstitutionCipher cipher = new SubstitutionCipher("BCDEFGHIJKLMNOPQRSTUVWXYZA");
System.out.println("Encryption code = " + new String(cipher.cc.encoder));
System.out.println("Decryption code = " + new String(cipher.cc.decoder));
  
String message = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
String coded = cipher.encrypt(message);
System.out.println("Secret: " + coded);
String answer = cipher.decrypt(coded);
System.out.println("Message: " + answer);
}

}

Please do just part C, part a and b done and I include them here with the original code. Test the result to make sure everything work fine and please type the code.

An important application of character arrays and strings is cryptography, which is the science of secret messages. This field involves the process of encryption, in which is message, called the plaintext, is converted into a scrambled message, called the ciphertext. Likewise, cryptography studies corresponding ways of performing decryption, turning a ciphertext back into its original plaintext. Arguably the earliest encryption scheme is the Caesar cipher, which is named after Julius Caesar, who used this scheme to protect important military messages. The Caesar cipher involves replacing each letter in a message with the letter that is a certain number of letters after it in the alphabet. So, in an English message, we might replace each A with D, each B with E, each C with F, and so on, if shifting by three characters. We continue this approach all the way up to W, which is replaced with Z. Then, we let the substitution pattern warp around, so that we replace X with A, Y with B, and Z with C. A) Modify the encryption/decryption code in chapter 3 so that the program can perform the Caesar cipher for English messages that include both upper- and lowercase characters B) Implement and test a class, Substitution Cipher, with a constructor that takes a string with the 26 upper case letters in an arbitrary order and uses that as the encoder for a cipher (that is, A mapped to the first character of the parameter, B is mapped to the second, and so on.) You should derive the decoding map from the forward version. C) Redesign the CaesarCipher class as a subclass of the SubstitutionCipher from the previous problem

Explanation / Answer

Hi,

Redesigning a class as a subclass of another class means to interhit the methods of that class. In our case we have to inherit the methods of SubstitutionCipher class to CaesarCipher class. This is done by using extends keyword. We are extending encrypt and decrypt method of Subsitution cipher. Please find code below;


public class CaesarCipherRedesign extends SubstitutionCipher{

   protected char[] encoder = new char[52];
   protected char[] decoder = new char[52];
  
      
   public CaesarCipherRedesign(String encode) {
       super(encode);
       int rotation=1;
       // TODO Auto-generated constructor stub
       for (int k = 0; k < 26; k++) {
           encoder[k] = (char) ('A' + (k + rotation) % 26);
           decoder[k] = (char) ('A' + (k - rotation + 26) % 26);
       }
       for (int k = 0; k < 26; k++) {
           encoder[k + 26] = (char) ('a' + (k + rotation) % 26);
           decoder[k + 26] = (char) ('a' + (k - rotation + 26) % 26);
       }
   }
  
  
  
   public String encrypt(String message) {
       return transform(message, encoder);
   }

   public String decrypt(String secret) {
       return transform(secret , decoder);
   }
  
   private String transform(String original, char[] code) {
       char[] msg = original.toCharArray();
       for (int k = 0; k < msg.length; k++) {
           if (Character.isUpperCase(msg[k])) {
               int j = msg[k] - 'A';
               msg[k] = code[j];
           } else {
               if (Character.isLowerCase(msg[k])) {
                   int j = msg[k] - 'a';
                   msg[k] = code[j + 26];
               }
           }
       }
       return new String(msg);
   }

   public static void main(String[] args) {
      
       CaesarCipherRedesign cipher = new CaesarCipherRedesign("BCDEFGHIJKLMNOPQRSTUVWXYZA");
       System.out.println("Encryption code = " + new String(cipher.encoder));
       System.out.println("Decryption code = " + new String(cipher.cc.decoder));

       String message = "HappY";
       String coded = cipher.encrypt(message);
       System.out.println("Secret: " + coded);
       String answer = cipher.decrypt(coded);
       System.out.println("Message: " + answer);
   }

}

In case of any query, please conect us.