凯撒大帝 Decyption/Encryption、Encoding/Decoding Java 计划

Julius Casear Decyption/Encryption, Encoding/Decoding Java Program

所以在我的程序中我应该有一个驱动程序 class 和实用程序 class。我必须编写一个程序,使用与 Caesar 相同类型的密码术。例如 a = f、b = g、c = h 等等。在我的驱动程序中 class 是我应该进行解码和编码过程的地方。该文件必须是 encrypted/decrypted 使用命令行参数。例如,

java CaesarLab 编码 "keyword" message.txt

在实用程序中 class 是字母移动的位置。这是我知道放置此代码的地方:

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

它还应该从 FileInputStream 中读取,并通过捕获所有 I/O 异常来处理 exception/errors。

这个程序似乎有很多组件,我无法将它们组合在一起。如果我能得到一些帮助来解决所有这些问题,我将不胜感激。

我制作了一个小骨架,让您具体了解那些 "many components" 可能是什么。您已经有了您的驱动程序 - 假设您在 CaesarDriver.java 中定义了它。

使用 "driver" 的实用程序现在看起来像这样:

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 :))

    }
}

让世界像这样被使用:

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 :)

    }
}

我希望这能支持您进一步深入研究任务 - 下次您遇到困难时,可以随时使用您的所有代码更新您的答案(它至少可以让您从现在起提出更具体的问题,所以你不会被否决。