示例#1
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));
  }
示例#2
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())));
 }