-4

So in my program I am supposed to have a driver class and utility class. I have to write a program that uses the same type of Cryptography as Caesar did. For example a = f, b = g, c = h, and so on. In my driver class is where I am supposed to have the decoding and encoding process. The file has to be encrypted/decrypted using command line arguments. For example,

java CaesarLab encode "keyword" message.txt

In the utility class is where the shifting of letters should be. This is where I know to put this code:

public static final int NUM_LETTERS = 26;

// shifting up for the encoding process
public static char shiftUpByK(char c, int k) {
   if ('a' <= c && c <= 'z')
       return (char) ('a' + (c - 'a' + k) % NUM_LETTERS);
   if ('A' <= c && c <= 'Z')
       return (char) ('A' + (c-'A' + k) % NUM_LETTERS);
   return c; // don't encrypt if not an alphabetic character
}

 // shifting down for the decoding process
 public static char shiftDownByK(char c, int k) {
     return shiftUpByK(c, NUM_LETTERS - k);
}

It should also read from a FileInputStream, and handle exception/errors by catching all I/O exceptions.

There seems to be a lot of components to this program and I am having trouble putting it all together. If i could get some help in sorting all of this out, it would be greatly appreciated.

whodywho
  • 3
  • 2
  • Having your "driver" i guess you are supposed to write a Utility class - for example having the methods `public String encodeCaesar(File f)` and `public String decodeCaesar(File f)` the utility could be used to encode/decode files (resp. their content) and will handle/ abstract the reading from the file (`FileInputStream`). I dont know the specific requirements so maybe you have additional utility-Methods such as `public void encriptWithCaesar(File sourceFile, File destinationFile)` or similar ones. – JBA Mar 04 '15 at 08:10
  • @JBA Yeah for ciphering it says to use `public static void encrypt (Scanner in, PrintWriter out, String key)` – whodywho Mar 04 '15 at 08:12
  • And for decrypting? (I will then post a answer containing a first hook where you could catch up - without implementing the whole "solution" of corse) – JBA Mar 04 '15 at 08:15
  • @JBA same line just with `decrypt` – whodywho Mar 04 '15 at 08:17
  • right (sorry my "crypto knowledge" is a bit rosty" ;)) – JBA Mar 04 '15 at 08:18
  • @JBA haha no worries! – whodywho Mar 04 '15 at 08:22
  • i need to quickly attend a meeting - giving you the rest in a minute :) – JBA Mar 04 '15 at 08:44
  • Caesar's cipher doesn't use a key, so what's this parameter supposed to be or do? - And what is the "keyword" in the java call supposed to be? Perhaps this is where "encode" or "decode" should be given? – laune Mar 04 '15 at 08:58

1 Answers1

-1

I made a little Skeleton for you to have a specific idea what those "many components" could be. You already have your driver - let's say you defined it in CaesarDriver.java.

A Utility using that "driver" could now look something like this:

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

/**
 * This is a utility class allows to encrypt/ decrypt the data received by a Scanner {@see Scanner} using the caesar
 * encoding by providing the key to be used.
 *
 * Note: Behind the scanner there may be a File or System.in as source
 */
public class CaesarUtility {

    /**
     * TODO: implement a mechanism to encrypt data ({@see Scanner}) and write the encrypted data out ({@see PrintWriter})
     * TODO: use the provided key for encryption
     * @param in source for the data to be encrypted {@see Scanner}
     * @param out destination for the encrypted data to be written to {@see PrintWriter}
     * @param key key to be used for the encryption
     */
    public static void encrypt (Scanner in, PrintWriter out, String key){

        // TODO: do we need to validate something? Could something be null? Do we need to care about this?

        // TODO: Read from Scanner
        String dataToEncrypt = "TODO: Read from Scanner";

        // TODO: Encryption
        String encryptedData = "Use your existing Caesar Driver with the correct method to encrypt the data";

        // TODO: Write encrpyted data using the provided PrintWriter
    }

    /**
     * TODO: The same like in encrypt but decrypt data this time
     * @param in source for the data to be decrypted {@see Scanner}
     * @param out destination for the decrypted data to be written to {@see PrintWriter}
     * @param key key to be used for the decryption
     */
    public static void decrypt (Scanner in, PrintWriter out, String key){

        // TODO: Basicially the same but for decryption (e.g. you will use your driver slightly different here :))

    }
}

Allowing the world to be used something like this:

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

/**
 *  Hello i am a Application that is too lazy to implement the Caesar encoding/ decoding - luckaly someone
 *  made me a Utility for that :)
 *
 *  TODO: This could need some improvement - maybe let the user type in the in/output file in the console?
 *
 */
public class Application {

    // TODO: How i said i am a bit rosty here - was it a number or a character - you know it better than i do i assume :)
    private static final String KEY = "???";

    public static void main(String[] args){
          new Application().start();
    }

    public void start(){

         // lets first encode a file - we need to have a Scanner to be able to use the Utility class (so i searched)
         // SO for "Java Scanner read File" and found this:
        File inputFile = new File("sensitive-data.txt");
        Scanner inputScanner = null;

        try {
            new Scanner(inputFile);
        } catch (FileNotFoundException e) {
            // TODO: Exception handling (does it make sence to proceed in this case? do we have to notify the user about this?
            e.printStackTrace();
        }

        // let the utility encode the file and write it to another file - luckaly searching SO for "Java PrintWriter example"
        // helps us out here
        File outFile = new File ("sensitive-data.txt");
        PrintWriter outWriter = null;
        try {
            outWriter = new PrintWriter (outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Alright we have both - the input as well as the output and the key - its time to let the utility do its work
        CaesarUtility.encrypt(inputScanner, outWriter, KEY);

        // TODO: Whoopy i accidentially overwritten the source file with encoded data - luckaly i am able to decode it again
        // TODO: Decode the encoded file before the boss asks about the next sensitive-data report :)

    }
}

I hope this supports you in further digging into the task - feel free to update your answer with all of your code when you're stuck next time (it will at least allow you to ask more specific from now on so you dont get downvoted to the ground).

Sinister Beard
  • 3,570
  • 12
  • 59
  • 95
JBA
  • 2,769
  • 5
  • 24
  • 40
  • To understand the current state of SO a bit better mind to explain me why you downvote me while trying to help? I mean its not like you would try to help here - currently it looks more like you're just hating around but a explanation may help me to understand it (i just care because this once was a good place to contribute and more and more blogposts seem to note a slight change regarding such actions - i must say i dont care about my rep or this account but more about your personal issues - maybe i can help there too ;)) – JBA Mar 04 '15 at 10:12
  • I dont find any reason why people downvote it,My upvote goes for you – SpringLearner Mar 04 '15 at 10:36
  • Here's a TL;DR version: Helping an individual is not the goal of SO; the goal of SO is to create a high-quality resource of programming knowledge (think wikipedia in Q/A format). Neither OPs question nor your "answer", which is more of a mentoring attempt anyways, are contributing towards this goal; thus they are _not useful_ which is explicitly mentioned as a downvote reason. Your profile mentions you want to help beginners (paraphrased), but then SO is maybe the wrong place for you - mentoring attempts such as yours are out of scope for SO and are better handled by traditional forums. – l4mpi Mar 04 '15 at 10:36
  • @SpringLearner care to explain what about this answer is upvote-worthy? Honest question - it does not meet SO answer criteria IMO. – l4mpi Mar 04 '15 at 10:36
  • @l4mpi As per my view this answer is useful so I have upvoted.There no specific rules to downvote/upvote apart from what mouse hover shows – SpringLearner Mar 04 '15 at 11:39
  • @SpringLearner of course, but I'm asking you to eplain _how_ it is useful. I can't see this answer being of use to anybody but OP. – l4mpi Mar 04 '15 at 11:54
  • @l4mpi spend some time in understanding the answer especially code and think as if you are a schoolboy and who is new to java – SpringLearner Mar 04 '15 at 12:00
  • Very many thanks for explaining - i do get the point and indeed SO seems the wrong place for beginners as well as people that do try to support them. As a consequence my 32 apprentice will delete their accounts and I myself will try to only answer "high-quality-questions" from now on. I think SO already is a high-quality-resource and i do understand you want to keep it that way which is good - just remind one day you're getting old and there might be no one keeping up the idear - like the 32 you just lost and will only "leech" instead of participate... – JBA Mar 04 '15 at 12:10
  • Maybe after nearly 10 years of using Stackoverflow (this is like my 5th account or so - i keept forgetting about them before there was the google login possibility :)) it is time for me to read the terms&conditions the first time. – JBA Mar 04 '15 at 12:21
  • 1
    @SpringLearner I decided to have a quick look-see after reading the preceding comments, and I must say that I have found several issues. (1) Code containing more TODO comments than solid LOCs (1st snippet). (2) The purpose of String KEY "=???" is unclear. (3) The statement `new Scanner(inputFile);` is useless except for throwing an exception. (4) Opening the input file also for output before the input has been read is a major bug. (The final TODO comments are just muddying the water.) (5) Why continue after printing a stack trace in the absence of an input file? - Wanting to help is OK, but... – laune Mar 04 '15 at 17:54
  • @laune wow you realy care which is nice :) Have a good one ;) – JBA Mar 06 '15 at 08:20