Question
7.8.1: Modify the program so that the user can specify an encryption key. Modify the following program so that the user can specify an encryption
7.8.1: Modify the program so that the user can specify an encryption key.
Modify the following program so that the user can specify an encryption key other than 3 with a -k option. For example:
java CaeserCipher -k15 input.txt output.txt
----------------------------------------------------------------------------------------------------------------------
CaesarCipher.java
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner;
/** This program encrypts a file using the Caesar cipher. */ public class CaesarCipher { public static void main(String[] args) throws FileNotFoundException { String[] my_args = new String[3]; Scanner console = new Scanner(System.in); my_args[0] = console.next(); my_args[1] = console.next(); my_args[2] = console.next();
final int DEFAULT_KEY = 3; int key = DEFAULT_KEY; String inFile = ""; String outFile = ""; int files = 0; // Number of command line arguments that are files
for (int i = 0; i < my_args.length; i++) { String arg = my_args[i]; if (arg.charAt(0) == '-') { // It is a command line option
char option = arg.charAt(1); if (option == 'd') { key = -key; } else if (option == 'k') { /* Your code goes here */ } else { usage(); return; } } else { // It is a file name files++; if (files == 1) { inFile = arg; } else if (files == 2) { outFile = arg; } } } if (files != 2) { usage(); return; }
Scanner in = new Scanner(new File(inFile)); in.useDelimiter(""); // Process individual characters PrintWriter out = new PrintWriter(outFile);
while (in.hasNext()) { char from = in.next().charAt(0); char to = encrypt(from, key); out.print(to); } in.close(); out.close(); }
/** Encrypts upper- and lowercase characters by shifting them according to a key. @param ch the letter to be encrypted @param key the encryption key @return the encrypted letter */ public static char encrypt(char ch, int key) { int base = 0; if ('A' <= ch && ch <= 'Z') { base = 'A'; } else if ('a' <= ch && ch <= 'z') { base = 'a'; } else { return ch; } // Not a letter int offset = ch - base + key; final int LETTERS = 26; // Number of letters in the Roman alphabet if (offset >= LETTERS) { offset = offset - LETTERS; } else if (offset < 0) { offset = offset + LETTERS; } return (char) (base + offset); }
/** Prints a message describing proper usage. */ public static void usage() { System.out.println("Usage: java CaesarCipher [-kKEY] [-d] infile outfile"); } }
infile1.txt
Veni, vidi, vici
infile2.txt
Experience is the teacher of all things
infile3.txt
Men freely believe that which they desire
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started