Example usage for java.security KeyPairGenerator generateKeyPair

List of usage examples for java.security KeyPairGenerator generateKeyPair

Introduction

In this page you can find the example usage for java.security KeyPairGenerator generateKeyPair.

Prototype

public KeyPair generateKeyPair() 

Source Link

Document

Generates a key pair.

Usage

From source file:de.pawlidi.openaletheia.utils.CipherUtils.java

/**
 * //  ww w .j  av  a  2 s  . com
 * @return
 */
public static KeyPair generateKeyPair() {
    KeyPairGenerator generator = null;
    SecureRandom secureRandom = null;
    try {
        generator = KeyPairGenerator.getInstance(CIPHER_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Could not generate key", e);
    }
    try {
        secureRandom = SecureRandom.getInstance(RANDOM_NUMBER_GENERATOR_ALGORITHM, "SUN");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        // ignore exception
    }
    if (secureRandom == null) {
        generator.initialize(2048);
    } else {
        generator.initialize(2048, secureRandom);
    }
    return generator.generateKeyPair();
}

From source file:com.trsst.Common.java

static final KeyPair generateSigningKeyPair() {
    try {/*www. ja v  a  2 s .c o m*/
        KeyPairGenerator kpg;
        // kpg = KeyPairGenerator.getInstance("EC", "BC");
        kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC();
        kpg.initialize(new ECGenParameterSpec(CURVE_NAME));
        KeyPair kp = kpg.generateKeyPair();
        return kp;
        // } catch (NoSuchAlgorithmException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
        // } catch (NoSuchProviderException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
    } catch (InvalidAlgorithmParameterException e) {
        log.error("Error while generating key: " + e.getMessage(), e);
    }
    return null;
}

From source file:com.trsst.Common.java

static final KeyPair generateEncryptionKeyPair() {
    try {//ww  w.  j  a va 2  s.  c  o m
        KeyPairGenerator kpg;
        // kpg = KeyPairGenerator.getInstance("EC", "BC");
        kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC();
        kpg.initialize(new ECGenParameterSpec(CURVE_NAME));
        KeyPair kp = kpg.generateKeyPair();
        return kp;
        // } catch (NoSuchAlgorithmException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
        // } catch (NoSuchProviderException e) {
        // e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static KeyPair generateRsaKey() throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(2048, new SecureRandom());

    return keyGen.generateKeyPair();
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static KeyPair generateKeyES256()
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-256");

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    keyGen.initialize(ecSpec, new SecureRandom());

    return keyGen.generateKeyPair();
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static KeyPair generateKeyES384()
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-384");

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    keyGen.initialize(ecSpec, new SecureRandom());

    return keyGen.generateKeyPair();
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static KeyPair generateKeyES512()
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-521");

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    keyGen.initialize(ecSpec, new SecureRandom());

    return keyGen.generateKeyPair();
}

From source file:test.integ.be.fedict.trust.util.TestUtils.java

public static KeyPair generateKeyPair(String algorithm)
        throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {

    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
    SecureRandom random = new SecureRandom();
    if ("RSA".equals(keyPairGenerator.getAlgorithm())) {
        keyPairGenerator.initialize(new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4), random);
    } else if (keyPairGenerator instanceof DSAKeyPairGenerator) {
        DSAKeyPairGenerator dsaKeyPairGenerator = (DSAKeyPairGenerator) keyPairGenerator;
        dsaKeyPairGenerator.initialize(512, false, random);
    }//from   www .  j  a  va 2s  .  co m
    return keyPairGenerator.generateKeyPair();
}

From source file:bftsmart.tom.util.RSAKeyPairGenerator.java

/**
 * Generate the key pair for the process with id = <id> and put it on the
 * files config/keys/publickey<id> and config/keys/privatekey<id>
 *
 * @param id the id of the process to generate key
 * @throws Exception something goes wrong when writing the files
 *///ww  w.  j  a  v a2  s  .com
public void run(int id, int size) throws Exception {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(size);
    KeyPair kp = keyGen.generateKeyPair();
    PublicKey puk = kp.getPublic();
    PrivateKey prk = kp.getPrivate();
    saveToFile(id, puk, prk);
}

From source file:cherry.foundation.crypto.RSASignatureSupportTest.java

private RSASignatureSupport createCrypto() throws Exception {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);// ww w .  j a v  a2s . c om
    KeyPair key = keygen.generateKeyPair();
    RSASignatureSupport crypto = new RSASignatureSupport();
    crypto.setAlgorithm("SHA256withRSA");
    crypto.setPublicKeyResource(new InMemoryResource(key.getPublic().getEncoded()));
    crypto.setPrivateKeyResource(new InMemoryResource(key.getPrivate().getEncoded()));
    crypto.afterPropertiesSet();
    return crypto;
}