Ejemplo n.º 1
0
  public static void main(String[] args) throws IOException {

    // reads in text file
    Scanner file = new Scanner(new FileReader("input.txt"));
    String message = file.useDelimiter("\\A").next();

    // capitalizes all letters and gets rid of blank spaces
    message = message.toUpperCase();
    message = message.replaceAll("\\s+", "");

    int cipher, encrypt_decrypt;

    // asks user of they want to encrypt or decrypt a message
    System.out.println("Do you want to encrypt or decrypt?");
    System.out.println("1 - Encrypt a message");
    System.out.println("2 - Decrypt a message");
    encrypt_decrypt = cin.nextInt();

    // asks user which algorithm to use
    System.out.println("Which cipher do you want to use?");
    System.out.println("1 - Shift Cipher");
    System.out.println("2 - Affine Cipher");
    System.out.println("3 - Substitution Cipher");
    System.out.println("4 - Vigenere Cipher");
    cipher = cin.nextInt();

    ciphers(message, cipher, encrypt_decrypt);
  }
Ejemplo n.º 2
0
  public static void ciphers(String message, int cipher, int encrypt_decrypt) {

    // determines which function to use depending on user input
    if (cipher == 1) {
      // asks user for a key
      System.out.println("Enter a key value: ");
      int k = cin.nextInt();
      if (encrypt_decrypt == 1) {
        shift(message, k);
      } else {
        shift(message, -k);
      }
    } else if (cipher == 2) {
      // asks user for a key
      System.out.println("Enter a key value: ");
      int k = cin.nextInt();

      // asks user for 'b' value
      System.out.println("Enter a 'b' value: ");
      int b = cin.nextInt();

      if (encrypt_decrypt == 1) {
        affine(message, k, b, 1);
      } else {
        affine(message, k, b, 2);
      }
    } else if (cipher == 3) {
      // asks user for a keyword & gets rid of repeated characters
      System.out.println("Enter a keyword: ");
      String keyword = cin.next();
      keyword = keyword.toUpperCase();
      keyword =
          keyword.replaceAll(
              (String.format("(.)(?<=(?:(?=\\1).).{0,%d}(?:(?=\\1).))", keyword.length())), "");

      if (encrypt_decrypt == 1) {
        substitution(message, keyword, 1);
      } else {
        substitution(message, keyword, 2);
      }
    } else if (cipher == 4) {
      // asks user for a keyword
      System.out.println("Enter a keyword: ");
      String keyword = cin.next();
      keyword = keyword.toUpperCase();

      if (encrypt_decrypt == 1) {
        vigenere(message, keyword, 1);
      } else {
        vigenere(message, keyword, 2);
      }
    }
  }