Example #1
0
  // -------------------- constructor -------------------------
  public CipherApp() {
    // Try to open the pad file.
    try {
      padFile = new FileReader("pad.txt");
      pad = new BufferedReader(padFile);
      pad.mark(1000);
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }

    // Print out the Menu options.
    System.out.println("+++++++++++++++++++++++++++++++++++" + "+++++++++++++++++++++++++");
    System.out.println(" ACME Cipher Machine " + "Started with pad: pad.txt");
    System.out.println("+++++++++++++++++++++++++++++++++++" + "+++++++++++++++++++++++++");
    System.out.println("Menu:");
    System.out.println("    (e)ncrypt  <cipher output file>");
    System.out.println("    (d)ecrypt  <cipher input file> ");
    System.out.println("    (q)uit");

    // Make the keyboard an input and continue through the loop while
    // the keyboard has a next line.
    keyboard = new Scanner(System.in);
    while (keyboard.hasNext()) {
      menu = keyboard.next();

      // If the user chooses quit, exit the loop.
      if (menu.equalsIgnoreCase("Quit") || menu.equalsIgnoreCase("q")) {
        System.out.println("Goodbye, don't forget " + "to burn the pad file!");
        break;
      }

      // If the user chooses Decrypt, decrypt the file they also input.
      else if (menu.equalsIgnoreCase("d") || menu.equalsIgnoreCase("decrypt")) {
        System.out.println("++++++++++++++++++++++++++++" + "++++++++++++++");
        System.out.println("Decrypting: ");
        System.out.println("++++++++++++++++++++++++++++" + "++++++++++++++");

        file = keyboard.next().trim();
        System.out.println("Input file (ciphertext): " + file + "\n");

        read = new File(file);
        try {

          decrypt = new Scanner(read);
          decrypting();
        } catch (Exception e) {
          System.out.println("Catch: " + e.getMessage());
        }
      }

      // If the user chooses Encrypt, Encrypt the input and save it to
      // a file.
      else if (menu.equalsIgnoreCase("e") || menu.equalsIgnoreCase("encrypt")) {
        System.out.println("++++++++++++++++++++++++++++" + "++++++++++++++");
        System.out.println("Encrypting: ");
        System.out.println("++++++++++++++++++++++++++++" + "++++++++++++++");

        file = keyboard.next().trim();
        System.out.println("Output file (plaintext): " + file + "\n");
        System.out.println("Input a message: ");
        try {
          out = new PrintWriter(file);
          encrypt = new Scanner(System.in);
          encrypting();
        } catch (Exception e) {
          System.out.println("Catch: " + e.getMessage());
        }
      }

      System.out.println("+++++++++++++++++++++++++++++++++++" + "+++++++++++++++++++++++++");
      System.out.println(" ACME Cipher Machine " + "Started with pad: pad.txt");
      System.out.println("+++++++++++++++++++++++++++++++++++" + "+++++++++++++++++++++++++");
      System.out.println("Menu:");
      System.out.println("    (e)ncrypt  <cipher output file>");
      System.out.println("    (d)ecrypt  <cipher input file> ");
      System.out.println("    (q)uit");
    }
  }