Question

In: Computer Science

Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...

Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions:

Objectives

  • Command line input
  • File input and output
  • Rethrowing exceptions

Program Description

Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should they intercept them. The cipher required that both Caesar and the recipient knew the key to the cipher.

You are to write a program that reads in a message from a text file, encodes the message with a Caesar cipher, and then outputs the message to another file. Your program should also be able to read in an encoded message and decode it.

  1. Define What this program must do. We did this in class. Check your class notes!

  2. Outline How this program will accomplish the task. Your solution should be modularized.

Specifications

Input

The program should read, in order, command line arguments.
- The filename of the text file to open
- The cipher key for encryption/decryption
- A digit: 1 to encode or 2 to decode

*Using an IDE, look for "Run configurations" to set arguments. The IDE arguments needed are only the filename, cipher key, and encode/decode digit. You don't need the "java Cipher" portion that would be required if truly running from the command line.

Example of full command line input to encrypt:

 java Cipher messageFile secretword 1    
 The result is a new encoded file called "messageFile.coded"  

Example of full command line input to decrypt:

 java Cipher messageFile.coded secretword 2  
 The result is a new decoded file called "messageFile.decoded"

Note that the decoded message in "messgeFile.decoded" should be identical to the original file "messageFile"

Errors and Exception handling

  • If the file does not exist, or the user somehow provides bad input data, the program should "crash", that is, rethrow the error to allow the default handler take care of it. (In the second version, this program will handle the errors with try/catch constructs.)
  • Only the digit 1 or 2 is allowed. Any other integer value will result in an error message and the program will terminate. For example, if the arguments are messageFile secretword 3, the error message will be:
    Option 3 is not valid
  • You may assume the cipher key is a single word of alphabet characters only.
  • You may assume the text in the message file has alphabetic characters and punctuation characters (spaces, exclamation points, periods) only.

Encryption and Decryption

  • To encrypt a message, each letter in the message is shifted right by a number corresponding to the distance of the cipher key letter from the first letter in the alphabet. The cipher key is reused over and over until the message is encrypted. Punctuation and spaces are not encrypted but still appear in the encoded message. Uppercase should be converted to lowercase. Letters "wrap" around from 'z' to 'a'.

  • Message: the ships sail
    Cipher key: swiftly
    Output: ldm lsgho xttj wb wluf

Message letter Alphabet value Cipher letter Alphabet Value Add Values / Wrap for Offset 'a' + Offset = Result
t 19 s 18 37 % 26 = 11 l
h 7 w 22 29 % 26 = 3 d
e 4 i 8 12 % 26 = 12 m
f none
s 18 t 19 37 % 26 = 11 l
h 7 l 11 18 % 26 = 18 s
i 8 y 24 32 % 26 = 6 g
p 15 s 18 33 % 26 = 7 h
s 18 w 22 40 % 26 = 14 o
i none
s 18 f 5 23 % 26 = 23 x
a 0 t 19 19 % 26 = 19 t
i 8 l 11 19 % 26 = 19 t
l 11 y 24 9 % 26 = 9 j

Other examples:

  • Message: the ships sail at dawn
    Cipher key: secretword
    Output: llg waedj kekc tp udor

  • Message: the ships sail at dawn
    Cipher key: aaa
    Output: the ships sail at dawn

Output

  • Output is written to a text file which is named same as the input file plus the extension:
    .coded if being encrypted or
    .decoded if being decrypted .

Helper Methods

You will want to add methods to shift up and down.

And this is the template provided:

/*
* Fix me
*/
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Cipher {

   public static final int NUM_LETTERS = 26;
   public static final int ENCODE = 1;
   public static final int DECODE = 2;

   public static void main(String[] args) /* FIX ME */ {

// letters
String alphabet = "abcdefghijklmnopqrstuvwxyz";
  
// Check args length, if error, print usage message and exit
if (args.length...

// Extract input args to variables
       String inputFilename =
       String key =
       int action =
       String outputFilename = getOutputFilename(inputFilename, action);
       Scanner input = openInput(inputFilename);
       PrintWriter output = openOutput(outputFilename);

// Read in data and output to file
// Convert all letters to lowercase for output

       // Close streams

   }

   /**
   * Open input for reading
   *
   * @param filename
   * @return Scanner
   * @throws FileNotFoundException
   */

  
   /**
   * Open output for writing
   *
   * @param filename
   * @return PrintWriter
   * @throws FileNotFoundException
   */


/**
* Encode letter by some offset d
*
* @param c input character
* @param offset amount to shift character value
* @return char encoded character
*/
   public static char shiftUpByK(char c, int distance) {
       if ('a' <= c && c <= 'z')
           return (char) ('a' + (c - 'a' + distance) % NUM_LETTERS);
       if ('A' <= c && c <= 'Z')
           return (char) ('A' + (c - 'A' + distance) % NUM_LETTERS);
       return c; // don't encrypt if not an ic character
   }
  
   /**
* Decode letter by some offset d
*
* @param c input character
* @param offset amount to shift character value
* @return char decoded character
*/

   /**
   * Changes file extension to ".coded" or ".decoded"
   *
   * @param filename
   * @return String new filename or null if action is illegal
   */


   public String getInfo() {
       return "Program 3, Student's name here";
   }

}

If someone could please help and use the template to code this in JAVA in the next couple of days it would be greatly appreciated, thank you!!!

Solutions

Expert Solution

Hi, I have written an implementation for the above scenario with appropriate comments wherever necessary for better understanding.

Note : in the below implementation input and output are as follows:

INPUT :   java Cipher messageFile.decoded secretword 1 ( to encode )

OUTPUT :   java Cipher messageFile.coded secretword 2 ( to dencode )

In the below implementation

key.charAt(i % key.length()) - 'a') gives us the next character of the key that is to be used to shift the character.

basically, we have to use mod operator that limits the integer values till key length.

Other functions should be straightforward to understand. If you find difficulty understanding anything, please drop a comment.

Following is the implementation:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Cipher {

    public static final int NUM_LETTERS = 26;
    public static final int ENCODE = 1;
    public static final int DECODE = 2;

    public static void main(String[] args) {

        // Check args length, if error, print usage message and exit
        if (args.length < 3) {
            System.out.println("Invalid arguments");
            System.exit(0);
        }
        // Extract input args to variables
        String inputFilename = args[0];
        String key = args[1];
        int action = Integer.parseInt(args[2]);

        // check if action is valid
        if(action!=1 || action!=2) {
            System.out.println("Option #"+action+" is not valid");
            System.exit(0);
        }

        // get output filename
        String outputFilename = getOutputFilename(inputFilename, action);

        // set input and output
        Scanner input = openInput(inputFilename);
        PrintWriter output = openOutput(outputFilename);

        // Read in data and output to file
        while (input.hasNextLine()) {
            // read next line
            String line = input.nextLine();
            String result = "";
            // if action is encode
            if (action == ENCODE) {
                result = encodeInput(line, key);
            } else {
                result = decodeInput(line, key);
            }
            System.out.println(result);
            // write to file
            output.write(result);
        }

        // Close streams
        input.close();
        output.close();

    }

    /**
     * Encode letter by some offset d
     *
     * @param c      input character
     * @param offset amount to shift character value
     * @return char encoded character
     */
    public static char shiftUpByK(char c, int distance) {
        if ('a' <= c && c <= 'z')
            return (char) ('a' + (c - 'a' + distance) % NUM_LETTERS);
        if ('A' <= c && c <= 'Z')
            return (char) ('A' + (c - 'A' + distance) % NUM_LETTERS);
        return c; // don't encrypt if not an ic character
    }

    /**
     * Decode letter by some offset d
     *
     * @param c      input character
     * @param offset amount to shift character value
     * @return char decoded character
     */
    public static char shiftDownByK(char c, int distance) {
        if ('a' <= c && c <= 'z')
            return (char) ('a' + (c - 'a' - distance + 26) % NUM_LETTERS);
        if ('A' <= c && c <= 'Z')
            return (char) ('A' + (c - 'A' - distance + 26) % NUM_LETTERS);
        return c; // don't encrypt if not an ic character
    }

    /**
     * Open input for reading
     *
     * @param filename
     * @return scanner object of input file to read the file
     * @throws FileNotFoundException
     */
    public static Scanner openInput(String inputFile) {
        File file = new File(inputFile);
        // Creating Scanner instnace to read File in Java
        try {
            return new Scanner(file);
        } catch (FileNotFoundException e) {
            System.out.println("invalid input file");
            e.printStackTrace();
        }
        return null;

    }

    /**
     * Open output for writing
     *
     * @param filename
     * @return PrintWriter
     * @throws FileNotFoundException
     */
    public static PrintWriter openOutput(String outputFile) {
        File file = new File(outputFile);
        try {
            return new PrintWriter(file);
        } catch (FileNotFoundException e) {
            System.out.println("invalid output filename");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Changes file extension to ".coded" or ".decoded"
     * 
     * @param filename
     * @return String new filename or null if action is illegal
     */
    public static String getOutputFilename(String input, int action) {
        // if file extension is not provided
        if (!input.contains(".")) {
            System.out.println("invalid input file passed");
            return null;
        } else {
            // get the file name
            String fileName = input.split("\\.")[0];
            if (action == ENCODE)
                return fileName + ".coded";
            else
                return fileName + ".decoded";
        }
    }

    // encode the text input using a key
    public static String encodeInput(String input, String key) {
        // initial result
        String result = "";
        for (int i = 0; i < input.length(); i++) {
            // to encode shift up by k
            char encodedLetter = shiftUpByK(input.charAt(i), key.charAt(i % key.length()) - 'a');
            // append the encoded character to the result
            result += encodedLetter;
        }
        // return result
        return result;
    }

    // decode the encoded input using a key
    public static String decodeInput(String input, String key) {
        // initial result
        String result = "";
        // loop throuh the input
        for (int i = 0; i < input.length(); i++) {
            // to decode shift down by k
            char encodedLetter = shiftDownByK(input.charAt(i), key.charAt(i % key.length()) - 'a');
            // append the decoded character to the result
            result += encodedLetter;
        }
        // return result
        return result;
    }

}

Related Solutions

Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES:...
Write a Java program to encrypt the following message using the RC4 cipher using key CODES: Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25 (hence CODES = [2 14 3 4 18]). Ignore spaces and punctuations and put...
Hello! If possible, could you provide how and in excel? I'm stuck and trying to learn...
Hello! If possible, could you provide how and in excel? I'm stuck and trying to learn so I can do well on the exam! A 20-year annuity pays $2,350 per month at the end of each month. If the discount rate is 13 percent compounded monthly for the first eight years and 10 percent compounded monthly thereafter, what is the present value of the annuity? (Do not round intermediate calculations and round your answer to 2 decimal places, e.g., 32.16.)...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT