Example usage for java.math BigInteger BigInteger

List of usage examples for java.math BigInteger BigInteger

Introduction

In this page you can find the example usage for java.math BigInteger BigInteger.

Prototype

public BigInteger(int bitLength, int certainty, Random rnd) 

Source Link

Document

Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);

    System.out.println(mod);/*  www .  j  av  a 2  s .  co m*/
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    int bitLength = 512; // 512 bits
    SecureRandom rnd = new SecureRandom();
    int certainty = 90; // 1 - 1/2(90) certainty
    System.out.println("BitLength : " + bitLength);
    BigInteger mod = new BigInteger(bitLength, certainty, rnd);
    BigInteger exponent = BigInteger.probablePrime(bitLength, rnd);
    BigInteger n = BigInteger.probablePrime(bitLength, rnd);

    BigInteger result = n.modPow(exponent, mod);
    System.out.println("Number ^ Exponent MOD Modulus = Result");
    System.out.println("Number");
    System.out.println(n);/*from  w w w .java2  s .  c o m*/
    System.out.println("Exponent");
    System.out.println(exponent);
    System.out.println("Modulus");
    System.out.println(mod);
    System.out.println("Result");
    System.out.println(result);
}

From source file:Main.java

static BigInteger[] generateSafePrimes(int size, int certainty, SecureRandom random) {
    BigInteger p, q;/* w ww.j  av  a  2  s  .c  o  m*/
    int qLength = size - 1;

    for (;;) {
        q = new BigInteger(qLength, 2, random);

        // p <- 2q + 1
        p = q.shiftLeft(1).add(ONE);

        if (p.isProbablePrime(certainty) && (certainty <= 2 || q.isProbablePrime(certainty))) {
            break;
        }
    }

    return new BigInteger[] { p, q };
}

From source file:RSA.java

/** Create an instance that can both encrypt and decrypt. */
public RSA(int bits) {
    bitlen = bits;/*w ww  . jav  a  2 s  .  co m*/
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
}

From source file:RSA.java

/** Generate a new public and private key set. */
public synchronized void generateKeys() {
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);/*from   www.j av a2 s . com*/
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
}