// call the 'decryptMessage' method in RSA class and decrypt cipher text message
  @FXML
  private void decryptCipher() {
    String cipherTextMessage = cipherText.getText();

    String encryptedMessage = rsa.decryptMessage(cipherTextMessage, publicKeyN, privateD);

    decryptedCipherText.setText(encryptedMessage);
  }
Beispiel #2
0
  public static void main(String[] args) throws IOException {
    /*
       Requirements:
       - BigInteger and SecureRandom should be used
        - p and q should be at least 512-bit
        - difference between p and q should be bigger than 2^256
        - demonstration of encryption and decryption should be given
        - should include encryption and decryption timing
        - public key e is 65537
    */

    RSA rsa = new RSA();
    int plainText;
    BigInteger b_plainText, b_cipherText;
    int e_start, e_end, encryptTime, d_start, d_end, decryptTime;

    if (args.length != 0 && args[0].equalsIgnoreCase("n")) {
      System.out.println("Plaintext chosen for you");
      plainText = 1325975912;
    } else {
      System.out.println("Enter a number (string characters not allowed): ");
      Scanner StdIn = new Scanner(System.in);
      plainText = StdIn.nextInt();
    }

    b_plainText = BigInteger.valueOf(plainText);
    e_start = (int) System.nanoTime();
    b_cipherText = rsa.encrypt(b_plainText);
    e_end = (int) System.nanoTime();

    System.out.println("plaintext:\t" + b_plainText.toString());
    System.out.println("ciphertext:\t" + b_cipherText.toString());

    d_start = (int) System.nanoTime();
    b_plainText = rsa.decrypt(b_cipherText);
    d_end = (int) System.nanoTime();
    System.out.println("plaintext after decryption:\t" + b_plainText.toString());

    encryptTime = e_end - e_start;
    decryptTime = d_end - d_start;
    System.out.println(
        "encryption time (ns): "
            + String.valueOf(encryptTime)
            + ", decryption time (ns): "
            + String.valueOf(decryptTime));
  }
  // generate prime numbers on 'Generate Prime Numbers' Button click
  @FXML
  private void generatePrimeNumbers() {
    bitsValue = valueBitsPrimary.getText(); // get a bits value entered by the user
    // firstPrime = firstPrimary.getText();
    // secondPrime = secondPrimary.getText();

    if (bitsValue.trim().equals("")) {

      makeAlert(
          "You have not selected a bits value!!",
          "Please select a bits value to generate prime number.");
      return; // if the bits value are not entered in the field, break the function and do not
      // proceed further
    }

    rsa.generatePrimes(bitsValue);

    // get first prime number
    firstPrimary.setText(rsa.getFirstPrime());
    // get second prime number
    secondPrimary.setText(rsa.getSecondPrime());
  }
Beispiel #4
0
 public static void main(String[] args) {
   RSA myRSA = new RSA();
   String text = "Ὅσον ζῇς φαίνου";
   System.out.println(text);
   System.out.println(myRSA.decrypt(myRSA.encrypt(text, myRSA.getPublicKey())));
 }
  // check if a numbers are prime on 'Check Prime Numbers' Button click
  @FXML
  private void checkPrimeNumbers() {
    boolean firstPrime =
        rsa.isPrime(
            firstPrimary.getText()); // check if the number is prime and assign a boolean value
    boolean secondPrime =
        rsa.isPrime(
            secondPrimary.getText()); // check if the number is prime and assign a boolean value

    if (firstPrime && secondPrime) { // boolean for both numbers is true
      rsa.setFirstPrime(firstPrimary.getText());
      rsa.setSecondPrime(secondPrimary.getText());

      publicKeyN = rsa.getModulus(); // get modulus (public key n)
      constantPHI = rsa.getPHI(); // get phi
      publicKeyE = rsa.getSecondPublicKey(); // get second public key e
      privateD = rsa.getPrivateKey(); // get private key

      modulusN.setText(publicKeyN);
      phi.setText(constantPHI);
      publicE.setText(publicKeyE);
      privateKeyD.setText(privateD);

      firstPrimary.setStyle(
          "-fx-border-color: green;"); // set border color to green if the number is a prime number
      secondPrimary.setStyle(
          "-fx-border-color: green;"); // set border color to green if the number is a prime number

      primeOrNotPrime1.setStyle("-fx-opacity: 1;");
      primeOrNotPrime2.setStyle("-fx-opacity: 1;");

      primeOrNotPrime1.setImage(
          new Image("images/green_accept.jpg")); // display green tick if the number is prime
      primeOrNotPrime2.setImage(new Image("images/green_accept.jpg"));

    } else if (!firstPrime && secondPrime) { // first number is not a prime number
      firstPrimary.setStyle(
          "-fx-border-color: red;"); // set border color to red if the number is not a prime number
      secondPrimary.setStyle(
          "-fx-border-color: green;"); // set border color to green if the number is a prime number

      primeOrNotPrime1.setStyle("-fx-opacity: 1;");
      primeOrNotPrime2.setStyle("-fx-opacity: 1;");

      primeOrNotPrime1.setImage(
          new Image("images/error.jpg")); // display red sign if the number is not a prime
      primeOrNotPrime2.setImage(new Image("images/green_accept.jpg"));

      // call 'makeAlert' function with appropriate message
      makeAlert(
          "Error!!",
          "The first number is not a prime number, please generate or select prime number!");

    } else if (firstPrime && !secondPrime) { // second number is not a prime number
      firstPrimary.setStyle(
          "-fx-border-color: green;"); // set border color to green if the number is a prime number
      secondPrimary.setStyle(
          "-fx-border-color: red;"); // set border color to red if the number is not a prime number

      primeOrNotPrime1.setStyle("-fx-opacity: 1;");
      primeOrNotPrime2.setStyle("-fx-opacity: 1;");

      primeOrNotPrime1.setImage(new Image("images/green_accept.jpg"));
      primeOrNotPrime2.setImage(new Image("images/error.jpg"));

      // call 'makeAlert' function with appropriate message
      makeAlert(
          "Error!!",
          "The second number is not a prime number, please generate or select prime number!");

    } else { // both numbers are not a prime numbers
      firstPrimary.setStyle(
          "-fx-border-color: red;"); // set border color to red if the number is not a prime number
      secondPrimary.setStyle(
          "-fx-border-color: red;"); // set border color to red if the number is not a prime number

      primeOrNotPrime1.setStyle("-fx-opacity: 1;");
      primeOrNotPrime2.setStyle("-fx-opacity: 1;");

      primeOrNotPrime1.setImage(new Image("images/error.jpg"));
      primeOrNotPrime2.setImage(new Image("images/error.jpg"));

      // call 'makeAlert' function with appropriate message
      makeAlert(
          "Error!!",
          "The both numbers are not a prime numbers, please generate or select prime numbers!");
    }
  }