Reputation: 89
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
Upvotes: 1
Views: 122
Reputation: 2532
If you are willing to use the 512
bits number and you want to randomly get it and hope it will be a prime. You will find no success with your kind of solution. This is just too large value to be handled with too many possibilities. Even if you use the BigInteger
constructor with certainty
in. Like so :
new BigInteger(int bitLength, int certainty, Random random);
Where the java doc says:
certainty
- a measure of the uncertainty that the caller is willing to tolerate. The probability that the new BigInteger represents a prime number will exceed (1 - 1/(2certainty)).
The larger you make certainty
, the smaller is the probablility that the number is not prime.
Still will not help you. You can consider on some point of taking just the next prime number if the randomed one is not a prime as @Olivier Grégoire suggested or just reduce the number of bits.
Upvotes: 2
Reputation: 35467
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);
}
}
Upvotes: 4
Reputation: 1
The value for maximum bitLength of the new BigInteger is very large. If you reduce it to a smaller number it will work. I'm referring to the value 512 in the code below. Try a small number like 12.
pRandom = new BigInteger(512, new Random());
Upvotes: 0