kmkmkmkmkm - 1 year ago 57
Java Question

# Nothing happens when running my code [BigInteger]

So I have a class that computes two prime numbers and then checks if they're prime

``````import java.util.*;
import java.math.*;

public class GeneratePrime{

public static BigInteger calculatePPrime() {
BigInteger pRandom;
while (true) {
pRandom = new BigInteger(512, new Random());
pRandom = pRandom.setBit(0);
if(isPrime(pRandom)){
System.out.println("Got Random Prime P: "+pRandom);
break;
}
}
return pRandom;
}

public static BigInteger calculateQPrime() {
BigInteger qRandom;
while(true){
qRandom = new BigInteger(512, new Random());
if(isPrime(qRandom)){
System.out.println("Got Random Prime Q: "+qRandom);
break;
}
}
return qRandom;
}

public static boolean isPrime(BigInteger number) {
if (!number.isProbablePrime(5))
return false;

BigInteger two = new BigInteger("2");
if (!two.equals(number) && BigInteger.ZERO.equals(number.mod(two)))
return false;

for (BigInteger i = new BigInteger("3"); i.multiply(i).compareTo(number) < 1; i = i.add(two)) {
if (BigInteger.ZERO.equals(number.mod(i)))
return false;
}
return true;
}
}
``````

This is more or less a conglomerate of things I have found on the internet. This class was originally not using any BigIntegers, but then I found out I am required to use it for my assignment, so I had to scratch everything and do this.

Anyway. I can't use any built in functions to calculate primes (idk if there even are any). My issue here is that when I run this code in:

``````import java.util.*;
import java.math.*;

public class RSA{
public static void main(String[] args) {
BigInteger p, q;
GeneratePrime gp = new GeneratePrime();
p = gp.calculatePPrime();
q = gp.calculateQPrime();
}
}
``````

Both classes compile fine, but then when I run the RSA class, nothing happens. No errors no nothing. My terminal is just blank. Does anyone know why? Or can anyone see if this code works on their machine? I know I've probably missed something dumb here. Thanks

You want to get random primes? Then make that clear in your code:

``````public BigInteger randomPrime(int bits, Random random) {
return new BigInteger(bits, random).nextProbablePrime();
// or
// return BigInteger.probablePrime(bits, random);
}
``````

Use the methods at your disposal, in this case: `nextProbablePrime()`. This method is so much more efficient than the set of methods you wrote. This will significantly boost your application.

Your RSA class can then become:

``````import java.util.*;
import java.math.*;

public class RSA{
public static void main(String[] args) {
BigInteger p, q;
GeneratePrime gp = new GeneratePrime();
Random random = new Random();
int bits = 512;
p = gp.randomPrime(bits, random);
q = gp.randomPrime(bits, random);
}
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download