Question

I want to implement RSA, and for that I need to generate e which should be gcd(e, ø(n)) = 1 and 1 < e < ø(n) and also should be very close in size to ø(n). My alg. below respects the first two steps, but the generated number is pretty small. How can I generate a bigger one?

    // generate random p,q,r on 512 bits
    p = new BigInteger(512, 15, new Random());
    q = new BigInteger(512, 15, new Random());
    r = new BigInteger(512, 15, new Random());

    // calculate n = p*q*r 
    n = p.multiply(q);
    n = n.multiply(r);

    //calculate ø(n) = (p - 1)*(q - 1)*(r - 1) 
    ø_n = p.subtract(BigInteger.valueOf(1));
    ø_n = ø_n.multiply(q.subtract(BigInteger.ONE));
    ø_n = ø_n.multiply(r.subtract(BigInteger.ONE));

    do {
    e = new BigInteger(2 * 512, new Random());

} while //while e >= ø_n
        ((e.compareTo(ø_n) >= 0)
        || //while gcd(e, ø(n)) != 1
        (e.gcd(ø_n).compareTo(BigInteger.ONE) != 0));

Check the while loop, everything else is just initialisations.

Was it helpful?

Solution

Consider using BigInteger.probablePrime() with SecureRandom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top