}
} */
public void generateKeyPair(final KeyPairCallback kp) {
BigInteger x = BigInteger.getBigInteger();
// accomodate odd key sizes in case anybody wants to use them
final int lp = (keySize + 1) >> 1;
final int lq = keySize - lp;
final BigInteger e = BigInteger.getBigInteger(publicExponent.toString());
com.allen_sauer.gwt.log.client.Log.debug("e : " + e);
// generate two random primes of size lp/lq
BigInteger.getProbablePrime(lp,new NumberGeneratedCallback(){
public void onGenerated(final BigInteger p_)
{
//final BigInteger p_ = new BigInteger(nbInteger.toString());
com.allen_sauer.gwt.log.client.Log.debug("p : " + p_);
BigInteger.getProbablePrime(lq,
new NumberGeneratedCallback(){
public void onGenerated(BigInteger q)
{
com.allen_sauer.gwt.log.client.Log.debug("q : " + q);
BigInteger p = p_;
if (p_.compareTo(q) < 0) {
BigInteger tmp = p;
p = q;
q = tmp;
}
BigInteger n = p.multiply(q);
// phi = (p - 1) * (q - 1) must be relative prime to e
// otherwise RSA just won't work ;-)
BigInteger p1 = p.subtract(BigInteger.ONE);
BigInteger q1 = q.subtract(BigInteger.ONE);
BigInteger phi = p1.multiply(q1);
// generate new p and q until they work. typically
// the first try will succeed when using F4
if (e.gcd(phi).compareTo(BigInteger.ONE) != 0) {
//repeat
generateKeyPair(kp);
return;
}
// private exponent d is the inverse of e mod phi
BigInteger d = e.modInverse(phi);
// 1st prime exponent pe = d mod (p - 1)
BigInteger pe = d.mod(p1);
// 2nd prime exponent qe = d mod (q - 1)
BigInteger qe = d.mod(q1);
// crt coefficient coeff is the inverse of q mod p
BigInteger coeff = q.modInverse(p);
try {
PublicKey publicKey = new RSAPublicKeyImpl(n, e);
PrivateKey privateKey =
new RSAPrivateCrtKeyImpl(n, e,d,p, q, pe, qe, coeff);