Exemplo n.º 1
0
  public void encrypting() {
    String plainString;
    char plainChar;
    int padInt = 0;
    char cipherChar;
    String newS = "";

    // while( encrypt.hasNextLine( ) )
    // {
    plainString = encrypt.nextLine();

    for (int i = 0; i < plainString.length(); i++) {
      plainChar = plainString.charAt(i);
      System.out.print(plainChar);
      try {
        padInt = pad.read();
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }
      cipherChar = (char) ((plainChar + padInt) % 128);
      newS = newS + cipherChar;
    }
    // }

    System.out.println("");
    System.out.println("Output  (plaintext): ");
    System.out.println(newS);
    out.print(newS);
    out.close();
    try {
      pad.reset();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
Exemplo n.º 2
0
  public void decrypting() {
    String cipherString;
    char cipherChar;
    int padInt = 0;
    char plainChar;
    String newS = "";

    while (decrypt.hasNextLine()) {
      cipherString = decrypt.nextLine();

      for (int i = 0; i < cipherString.length(); i++) {
        cipherChar = cipherString.charAt(i);
        System.out.print(cipherChar);
        try {
          padInt = pad.read();
        } catch (Exception e) {
          System.out.println(e.getMessage());
        }
        plainChar = (char) ((cipherChar - padInt + 128) % 128);
        newS = newS + plainChar;
      }
    }

    System.out.println("");
    System.out.println("Output  (plaintext): ");
    System.out.println(newS);
    try {
      pad.reset();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
Exemplo n.º 3
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");
    }
  }