Question: To use a cipher we might have a class, say CipherMachine, wherein the specific cipher to be used is provided via the constructor: public CipherMachine(Encryptable
To use a cipher we might have a class, say CipherMachine, wherein the specific cipher to be used is provided via the constructor: public CipherMachine(Encryptable e)
Because we want to handle numerous ciphers, the specific cipher is provided at the time of construction. For this project, we will use only two: Ceasar Cipher and Rotating Key Ceasar Cipher.
For the purposes of this project, he has decided that the program will be capable of the following tasks: 1. To encode a file. 2. To decode a file. 3. To repeat steps 1 and/or 2 as many times as necessary.
Because users have such great difficulty with entering data correctly, Professor X has decided to automate the process by having the main method reading its required parameters (see below) as command line arguments. The user will NOT be prompted for any input.
Ultimately, the program that will operate as follows: 1. Via the command line, main will be provided with several jobs to process. Each job, an element in the args array, will specify the type of encryption, the name of an encrypted file, the decryption key, and, optionally an output file for the encrypted/decrypted text. 2. The cipher will be designated by the c flag followed by a cipher name, the input file will be designated by the i flag followed by a file name, and the output file by the o flag followed by its file name. For example, -cciphername -ooutputfile.txt -iinputfile.txt k 5 2 With the exception of the key (the -k flag which must be last), order does not matter, so the following is also acceptable: -iinputfile.txt -cciphername -ooutputfile.txt k 5 2 3. If the -d flag is present, the input file will be decrypted. 4. If an output file is not designated, information is written to the console (System.out). 5. If an input file is not designated, information is read from the console (System.in). 6. Command input is not case sensitive. 7. The user will NOT be prompted for any input. 8. For every character of input in the input file, encrypt or decrypt it and output it according to steps 2 through 4. 9. The two cipher classes supported are CeasarCipher and CeasarCipher2
Sample jobs might be Using a Ceasar cipher with a key of 5, encrypt plaintext.txt to the file encryptedtext.txt -iplaintext.txt -oencyptedtext.txt -cCeasar -k 5 Using a rotating Ceasar cipher with a key of 5 4 7 2 1, encrypt plaintext.txt to the file encryptedtext2.txt -cCeasar2 -oencyptedtext2.txt -iplaintext.txt -k 5 4 7 2 1 Using a Ceasar cipher with a key of 5, decrypt encryptedtext.txt and output to the console. -cCeasar -iencyptedtext.txt -d -k 5
PLEASE HELP ME OR AT LEAST GUIDE ME. BELOW IS STARTING CODE
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}
public class Encryptable {
/**
* Encrypt a single character
* @param plainText
* @return encrypted character
*/
default char encrypt(char plainText) {
return plainText;
}
/**
* Decrypt a single character
* @param encryptedText
* @return plainText character
*/
default char decrypt(char encryptedText) {
return encryptedText;
}
/**
* Establish encryption/decryption key
* @param key String containing the key
*/
void setKey(String key);
/**
* Reset cipher. Used for those that manipulate the key or
* are position dependent.
*/
void reset();
}
public interface CipherMachineInterface {
/**
* Establish input file name
* @param filename
* @return true if successful
*/
boolean setInput(String filename);
/**
* Establish output file name
* @param filename
* @return true if successful
*/
boolean setOutput(String filename);
/**
* Establish cipher key
* @param key
*/
void setKey(String key);
/**
* Reset the cipher class to default settings
*/
void reset();
/**
* Execute encryption or decryption
* @param encrypt if true, decrypt if false.
* @return true if successful
*/
boolean execute(boolean encrypt);
}
public class CipherMachine implements CipherMachineInterface {
public CipherMachine(Encryptable e) {
}
@Override
public boolean setInput(String filename) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean setOutput(String filename) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void setKey(String key) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void reset() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public boolean execute(boolean encrypt) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;
public class CeasarCipher implements Encryptable {
private final static int DEFAULT_KEY = 0; private int key;
/** * By default, the key = DEFAULT_KEY; */ public CeasarCipher() { this.key = DEFAULT_KEY; }
/** * Establish the key shift * * @param key */ public CeasarCipher(int key) { this.key = key; }
/** * Need to override the default method to perform the actual decryption * @param encryptedText * @return decrypted text */ @Override public char decrypt(char encryptedText) { char dText = encrypt(encryptedText, -key); return dText; }
/** * Need to override the default method to perform the actual encryption * @param plainText * @return encrypted text */ @Override public char encrypt(char plainText) { char eText = encrypt(plainText, key); return eText; }
/** * Encrypts/decrypts characters from a space to ~ by shifting them according * to a key. * * @param ch the letter to be encrypted * @param key the encryption key * @return the encrypted letter */ private static char encrypt(char ch, int key) { final int base = ' '; // The space char final int range = '~' - base; int shiftedChar = ch + key - base; if (shiftedChar > range) { // The ~ shiftedChar %= range; // Wrap around } else if (shiftedChar < 0) { shiftedChar += range; } return (char) (shiftedChar + base); }
@Override public void setKey(String key) { this.key = Integer.parseInt(key); }
@Override public void reset() { }
}
public class CaesarCipher2 implements Encryptable {
private Queue
public CeasarCipher2() { this.key = "0"; reset(); }
@Override public char encrypt(char plainText) { int keyValue = encryptQueue.remove(); char encoded = (char) ((int) plainText + keyValue); encryptQueue.add(keyValue); return encoded; }
@Override public char decrypt(char encryptedText) { int keyValue = decryptQueue.remove(); char decoded = (char) ((int) encryptedText + keyValue); decryptQueue.add(keyValue); return decoded; }
@Override public void setKey(String key) { /** * load key queue from a string of numbers */ Scanner keys = new Scanner(key); while (keys.hasNextInt()) { int k = keys.nextInt(); encryptQueue.add(k); decryptQueue.add(-k); }
}
@Override public void reset() { encryptQueue = new LinkedList
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
