Search in sources :

Example 1 with AsymmetricCipherKeyPair

use of org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair in project BiglyBT by BiglySoftware.

the class DHKeyPairGenerator method generateKeyPair.

@Override
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger p, g, x, y;
    int qLength = param.getStrength() - 1;
    DHParameters dhParams = param.getParameters();
    p = dhParams.getP();
    g = dhParams.getG();
    // 
    // calculate the private key
    // 
    x = new BigInteger(qLength, param.getRandom());
    // 
    // calculate the public key.
    // 
    y = g.modPow(x, p);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhParams), new DHPrivateKeyParameters(x, dhParams));
}
Also used : DHPublicKeyParameters(org.gudy.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.gudy.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 2 with AsymmetricCipherKeyPair

use of org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair in project BiglyBT by BiglySoftware.

the class ECKeyPairGenerator method generateKeyPair.

/**
 * Given the domain parameters this routine generates an EC key
 * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
 */
@Override
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger n = params.getN();
    int nBitLength = n.bitLength();
    BigInteger d;
    do {
        d = new BigInteger(nBitLength, random);
    } while (d.equals(ZERO) || (d.compareTo(n) >= 0));
    ECPoint Q = params.getG().multiply(d);
    return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(Q, params), new ECPrivateKeyParameters(d, params));
}
Also used : ECPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters) BigInteger(java.math.BigInteger) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) ECPublicKeyParameters(org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters) ECPoint(org.gudy.bouncycastle.math.ec.ECPoint) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 3 with AsymmetricCipherKeyPair

use of org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair in project BiglyBT by BiglySoftware.

the class DSAKeyPairGenerator method generateKeyPair.

@Override
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger p, q, g, x, y;
    DSAParameters dsaParams = param.getParameters();
    SecureRandom random = param.getRandom();
    q = dsaParams.getQ();
    p = dsaParams.getP();
    g = dsaParams.getG();
    do {
        x = new BigInteger(160, random);
    } while (x.equals(ZERO) || x.compareTo(q) >= 0);
    // 
    // calculate the public key.
    // 
    y = g.modPow(x, p);
    return new AsymmetricCipherKeyPair(new DSAPublicKeyParameters(y, dsaParams), new DSAPrivateKeyParameters(x, dsaParams));
}
Also used : DSAPublicKeyParameters(org.gudy.bouncycastle.crypto.params.DSAPublicKeyParameters) DSAPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.DSAPrivateKeyParameters) BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) DSAParameters(org.gudy.bouncycastle.crypto.params.DSAParameters) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)

Example 4 with AsymmetricCipherKeyPair

use of org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair in project BiglyBT by BiglySoftware.

the class RSAKeyPairGenerator method generateKeyPair.

@Override
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger p, q, n, d, e, pSub1, qSub1, phi;
    // 
    // p and q values should have a length of half the strength in bits
    // 
    int pbitlength = (param.getStrength() + 1) / 2;
    int qbitlength = (param.getStrength() - pbitlength);
    e = param.getPublicExponent();
    // 
    for (; ; ) {
        p = new BigInteger(pbitlength, 1, param.getRandom());
        if (p.mod(e).equals(ONE)) {
            continue;
        }
        if (!p.isProbablePrime(param.getCertainty())) {
            continue;
        }
        if (e.gcd(p.subtract(ONE)).equals(ONE)) {
            break;
        }
    }
    // 
    for (; ; ) {
        // 
        for (; ; ) {
            q = new BigInteger(qbitlength, 1, param.getRandom());
            if (q.equals(p)) {
                continue;
            }
            if (q.mod(e).equals(ONE)) {
                continue;
            }
            if (!q.isProbablePrime(param.getCertainty())) {
                continue;
            }
            if (e.gcd(q.subtract(ONE)).equals(ONE)) {
                break;
            }
        }
        // 
        // calculate the modulus
        // 
        n = p.multiply(q);
        if (n.bitLength() == param.getStrength()) {
            break;
        }
        // 
        // if we get here our primes aren't big enough, make the largest
        // of the two p and try again
        // 
        p = p.max(q);
    }
    if (p.compareTo(q) < 0) {
        phi = p;
        p = q;
        q = phi;
    }
    pSub1 = p.subtract(ONE);
    qSub1 = q.subtract(ONE);
    phi = pSub1.multiply(qSub1);
    // 
    // calculate the private exponent
    // 
    d = e.modInverse(phi);
    // 
    // calculate the CRT factors
    // 
    BigInteger dP, dQ, qInv;
    dP = d.remainder(pSub1);
    dQ = d.remainder(qSub1);
    qInv = q.modInverse(p);
    return new AsymmetricCipherKeyPair(new RSAKeyParameters(false, n, e), new RSAPrivateCrtKeyParameters(n, e, d, p, q, dP, dQ, qInv));
}
Also used : BigInteger(java.math.BigInteger) RSAKeyParameters(org.gudy.bouncycastle.crypto.params.RSAKeyParameters) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair) RSAPrivateCrtKeyParameters(org.gudy.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)

Example 5 with AsymmetricCipherKeyPair

use of org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair in project BiglyBT by BiglySoftware.

the class DHBasicKeyPairGenerator method generateKeyPair.

@Override
public AsymmetricCipherKeyPair generateKeyPair() {
    BigInteger p, g, x, y;
    int qLength = param.getStrength() - 1;
    DHParameters dhParams = param.getParameters();
    p = dhParams.getP();
    g = dhParams.getG();
    // 
    // calculate the private key
    // 
    x = new BigInteger(qLength, param.getRandom());
    // 
    // calculate the public key.
    // 
    y = g.modPow(x, p);
    return new AsymmetricCipherKeyPair(new DHPublicKeyParameters(y, dhParams), new DHPrivateKeyParameters(x, dhParams));
}
Also used : DHPublicKeyParameters(org.gudy.bouncycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.gudy.bouncycastle.crypto.params.DHParameters) DHPrivateKeyParameters(org.gudy.bouncycastle.crypto.params.DHPrivateKeyParameters) BigInteger(java.math.BigInteger) AsymmetricCipherKeyPair(org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)

Aggregations

BigInteger (java.math.BigInteger)6 AsymmetricCipherKeyPair (org.gudy.bouncycastle.crypto.AsymmetricCipherKeyPair)6 DHParameters (org.gudy.bouncycastle.crypto.params.DHParameters)2 DHPrivateKeyParameters (org.gudy.bouncycastle.crypto.params.DHPrivateKeyParameters)2 DHPublicKeyParameters (org.gudy.bouncycastle.crypto.params.DHPublicKeyParameters)2 SecureRandom (java.security.SecureRandom)1 DSAParameters (org.gudy.bouncycastle.crypto.params.DSAParameters)1 DSAPrivateKeyParameters (org.gudy.bouncycastle.crypto.params.DSAPrivateKeyParameters)1 DSAPublicKeyParameters (org.gudy.bouncycastle.crypto.params.DSAPublicKeyParameters)1 ECPrivateKeyParameters (org.gudy.bouncycastle.crypto.params.ECPrivateKeyParameters)1 ECPublicKeyParameters (org.gudy.bouncycastle.crypto.params.ECPublicKeyParameters)1 ElGamalParameters (org.gudy.bouncycastle.crypto.params.ElGamalParameters)1 ElGamalPrivateKeyParameters (org.gudy.bouncycastle.crypto.params.ElGamalPrivateKeyParameters)1 ElGamalPublicKeyParameters (org.gudy.bouncycastle.crypto.params.ElGamalPublicKeyParameters)1 RSAKeyParameters (org.gudy.bouncycastle.crypto.params.RSAKeyParameters)1 RSAPrivateCrtKeyParameters (org.gudy.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters)1 ECPoint (org.gudy.bouncycastle.math.ec.ECPoint)1