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

Code Please do both of part a and b and test the result to make sure everything

ID: 3793986 • Letter: C

Question

Code

Please do both of part a and b and test the result to make sure everything work fine.One thing please type it.

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. 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, Bis mapped to the second, and so on. You should derive the decoding map from the forward version.

Explanation / Answer

// 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;
protected char[] encoder = new char[26];
protected char[] decoder = new char[26];
  
public SubstitutionCipher(String subs)
{
char[] code = subs.toCharArray();
for(int k = 0; k < 26; k++) {
encoder[k] = code[k];
decoder[encoder[k] - 'A'] = encoder[k];
}
  
}
  
public String encrypt(String message)
{
char[] msg = message.toCharArray();
for(int k = 0; k < msg.length; k++){
if (Character.isUpperCase(msg[k])) {
int j = ((msg[k] - 'A'))%26;
msg[k] = encoder[j];
}
}
return new String(msg);
}
  
public String decrypt(String secret)
{
char[] msg = secret.toCharArray();
for(int k = 0; k < msg.length; k++){
if (Character.isUpperCase(msg[k])) {
int j = ((msg[k] - 'A'))%26;
msg[k] = encoder[decoder[j] - 'A'];
}
}
return new String(msg);
}
  
public static void main(String[] args)
{
SubstitutionCipher cipher = new SubstitutionCipher("EFGHABCDIJKLMNOPQRSTUVWXYZ");
System.out.println("Encryption code = " + new String(cipher.encoder));
System.out.println("Decryption code = " + new String(cipher.decoder));
  
String message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String coded = cipher.encrypt(message);
System.out.println("Secret: " + coded);
String answer = cipher.decrypt(coded);
System.out.println("Message: " + answer);
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote