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

How would I add code to encrypt this code below and then how would I add additio

ID: 3600119 • Letter: H

Question

How would I add code to encrypt this code below and then how would I add additional code to decrypt the file? What I need to do is use this version of substitution ciphers as well below;

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

F E A T H R Z Y X W V U S Q P O N M L K J I G D C B

It should ask the user for the key word that the user wants to use. Also my program should read text from the input file, encrypt or decrypt it according to the user’s request, and write the result to an output file. If the user chooses to do encryption, your program will produce an output file containing English text that is encrypted with the key the user provides. And then if the user runs the program again for decryption, your program should be able to use the same key (provided by the user) to decrypt the encrypted file and produce an output file that is identical to the original file before encryption is done.

What I have so far below

------------------

--------------------------------

what I have for testing below

--------------------------------

Explanation / Answer

Below is the working program of ceaser cipher. Please modify the code you wrote for testing the Cipher, read input file one line at a time and call encrypt method by passing that line and write the cipher text returned by it to the output file. Do the vice versa for the decryption.

/*ceaser cipher*/

import java.io.InputStream;

import java.io.OutputStream;

import java.io.IOException;

import java.lang.*;

/**

This class encrypts files using the Caesar cipher.

For decryption, use an encryptor whose key is the

negative of the encryption key.

*/

public class Cipher

{

private static int shiftKey;

public static final String ALPHABET1 = "abcdefghijklmnopqrstuvwxyz";

public static final String ALPHABET2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/**

Encrypts the contents of a stream.

@param in plaintext will take a string of plain text

@param out the encrypted output string

*/   

public String encrypt(String plainText, int shiftKey){

  

String cipherText = "";

for (int i = 0; i < plainText.length(); i++)

{

if(Character.isUpperCase(plainText.charAt(i)) && Character.isLetter(plainText.charAt(i))){

int charPosition = ALPHABET2.indexOf(plainText.charAt(i));

int keyVal = (shiftKey + charPosition) % 26;

char replaceVal = ALPHABET2.charAt(keyVal);

cipherText += replaceVal;

}

else if(Character.isLowerCase(plainText.charAt(i)) && Character.isLetter(plainText.charAt(i))){

int charPosition = ALPHABET1.indexOf(plainText.charAt(i));

int keyVal = (shiftKey + charPosition) % 26;

char replaceVal = ALPHABET1.charAt(keyVal);

cipherText += replaceVal;

}

else{

cipherText += plainText.charAt(i);

}

}

return cipherText;

}

public String decrypt(String cipherText, int shiftKey)

{

String plainText = this.encrypt(cipherText, 26-shiftKey);

return plainText;

}

/*public static void main(String[] s){

String str="My Name is Khan";

Cipher cipher = new Cipher();

String ct=cipher.encrypt(str,3);

System.out.println(ct);

String ct1=cipher.decrypt(ct,3);

System.out.println(ct1);

}*/  

}

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