Reputation: 91
I want to implement the DSA (Digital Signature Algorithm) algorithm using Java. When building the p and q keys, I want the result of (p-1) mod q = 0. This is the code that I made, when I call the contents of q2 (result of p-1 mod q) it doesn't return 0.
package random;
import java.util.Random;
import java.math.BigInteger;
public class RandomPrime implements Runnable {
BigInteger randomNumber = BigInteger.probablePrime(512, new Random());
BigInteger randomNumber2 = BigInteger.probablePrime(160, new Random());
BigInteger p = randomNumber;
BigInteger q = randomNumber2;
BigInteger q2 = p.subtract(BigInteger.ONE).remainder(q);
@Override
public void run() {
while(!q2.equals(BigInteger.ZERO)){
randomNumber = BigInteger.probablePrime(512, new Random());
randomNumber2 = BigInteger.probablePrime(160, new Random());
p = randomNumber;
q = randomNumber2;
q2 = p.subtract(BigInteger.ONE).remainder(q);
}
}
public BigInteger getPValue() {
return p;
}
public BigInteger getQValue() {
return q;
}
public BigInteger getQ2Value() {
return q2;
}
}
This is how I call it
RandomPrime rPrime = new RandomPrime();
Thread thread = new Thread(rPrime);
thread.start();
BigInteger p = rPrime.getPValue();
BigInteger q = rPrime.getQValue();
BigInteger q2 = rPrime.getQ2Value();
System.out.println("p:" +p);
System.out.println("q:" +q);
System.out.println("q2:" +q2);
Upvotes: 1
Views: 472
Reputation: 51393
You start the thread:
Thread thread = new Thread(rPrime);
thread.start();
and you immediately call for the result:
BigInteger p = rPrime.getPValue();
BigInteger q = rPrime.getQValue();
BigInteger q2 = rPrime.getQ2Value();
System.out.println("p:" +p);
System.out.println("q:" +q);
System.out.println("q2:" +q2);
You did not give enough time for the thread to actually finish to performed its assigned work. In your case calling thread.join()
The join method allows one thread to wait for the completion of another.
after the start would be enough, i.e., making the initial thread to wait for the second thread to finish, namely:
RandomPrime rPrime = new RandomPrime();
Thread thread = new Thread(rPrime);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// do something
}
BigInteger p = rPrime.getPValue();
BigInteger q = rPrime.getQValue();
BigInteger q2 = rPrime.getQ2Value();
System.out.println("p:" +p);
System.out.println("q:" +q);
System.out.println("q2:" +q2);
Upvotes: 3
Reputation: 73
I'm not familiar with the algorithm you're implementing. But there is at least the following problem:
You are not waiting for the thread to finish. One solution for this could be to add a thread.join()
immediately after your call to thread.start()
. However, this makes the separate thread kind of useless since your main thread does not do any work while the new thread is running.
Upvotes: 0