Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

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"); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Publishing With Filemaker Pro On The Web

Authors: Maria Langer

1st Edition

0201696657, 978-0201696653

More Books

Students also viewed these Databases questions

Question

What is a DNA library? Do you think this is an appropriate name?

Answered: 1 week ago

Question

5. What are the two key assumptions of self-expansion theory?

Answered: 1 week ago