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:org.cogroo.addon.util.SecurityUtil.java

public KeyPair genKeyPair() {
    KeyPair kpr = null;// www  .j  a v a 2s .c  om
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(new RSAKeyGenParameterSpec(RSAKEYSIZE, RSAKeyGenParameterSpec.F4));
        kpr = kpg.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        LOG.log(Level.SEVERE, "Error generating key pair", e);
    } catch (InvalidAlgorithmParameterException e) {
        LOG.log(Level.SEVERE, "Error generating key pair", e);
    }
    return kpr;
}

From source file:mitm.common.security.ca.SMIMECABuilder.java

private KeyPair generateKeyPair(int keyLength) throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA");

    keyPairGenerator.initialize(keyLength, randomSource);

    return keyPairGenerator.generateKeyPair();
}

From source file:edu.vt.alerts.android.library.tasks.RegistrationTask.java

private KeyPair generateKeyPair() throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYPAIR_ALGORITHM);
    kpg.initialize(KEYPAIR_KEYSIZE);/*from w  ww  .  j a  va2  s  .  c  om*/
    return kpg.generateKeyPair();
}

From source file:edu.vt.middleware.crypt.util.CryptReaderWriterTest.java

/**
 * @return  Public key test data.//from   ww w.  j  a  va 2s. c  o m
 *
 * @throws  Exception  On test data generation failure.
 */
@DataProvider(name = "pubkeydata")
public Object[][] createPubKeyTestData() throws Exception {
    final KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA");
    final KeyPair rsaKeys = rsaKeyGen.generateKeyPair();
    final KeyPairGenerator dsaKeyGen = KeyPairGenerator.getInstance("DSA");
    final KeyPair dsaKeys = dsaKeyGen.generateKeyPair();

    return new Object[][] { { rsaKeys.getPublic() }, { dsaKeys.getPublic() }, };
}

From source file:edu.vt.middleware.crypt.util.CryptReaderWriterTest.java

/**
 * @return  Private key test data./* w ww .  jav  a 2 s  .  c  o m*/
 *
 * @throws  Exception  On test data generation failure.
 */
@DataProvider(name = "privkeydata")
public Object[][] createPrivKeyTestData() throws Exception {
    final KeyPairGenerator rsaKeyGen = KeyPairGenerator.getInstance("RSA");
    final KeyPair rsaKeys = rsaKeyGen.generateKeyPair();
    final KeyPairGenerator dsaKeyGen = KeyPairGenerator.getInstance("DSA");
    final KeyPair dsaKeys = dsaKeyGen.generateKeyPair();
    return new Object[][] { { rsaKeys.getPrivate(), "S33Kr1t!" }, { dsaKeys.getPrivate(), "S33Kr1t!" },
            { rsaKeys.getPrivate(), null }, { dsaKeys.getPrivate(), null }, };
}

From source file:net.solarnetwork.pki.bc.test.BCCertificateServiceTest.java

@Before
public void setup() {
    service = new BCCertificateService();
    KeyPairGenerator keyGen;
    try {//from w  ww . j  av a2 s .  c  o m
        keyGen = KeyPairGenerator.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    keyGen.initialize(2048, new SecureRandom());
    KeyPair keypair = keyGen.generateKeyPair();
    publicKey = keypair.getPublic();
    privateKey = keypair.getPrivate();
}

From source file:test.unit.org.owasp.webscarab.util.SunCertificateUtilsTest.java

private KeyPair generateKeyPair(int size) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    SecureRandom random = new SecureRandom();
    keyPairGenerator.initialize(new RSAKeyGenParameterSpec(size, RSAKeyGenParameterSpec.F4), random);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    return keyPair;
}

From source file:org.apache.sshd.server.keyprovider.AbstractGeneratorHostKeyProvider.java

private KeyPair generateKeyPair(String algorithm) {
    try {/*from   w  w w .  j  a  va2  s .com*/
        KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator(algorithm);
        if (keySize != 0) {
            generator.initialize(keySize);
        }
        LOG.info("Generating host key...");
        KeyPair kp = generator.generateKeyPair();
        return kp;
    } catch (Exception e) {
        LOG.error("Unable to generate keypair", e);
        return null;
    }
}

From source file:wsattacker.library.signatureFaking.helper.CertificateHandler.java

public void createFakedCertificate() throws CertificateHandlerException {
    try {//from  w w w .ja v  a2  s.  c o m
        log.debug("Faking the found certificate");
        // TODO: implement this with bouncy castle
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(originalPublicKey.getAlgorithm());
        kpg.initialize(((RSAPublicKey) certificate.getPublicKey()).getModulus().bitLength());
        fakedKeyPair = kpg.generateKeyPair();

        X509CertInfo info = new X509CertInfo();
        CertificateValidity interval = new CertificateValidity(certificate.getNotBefore(),
                certificate.getNotAfter());
        // TODO: new SecureRandom().generateSeed(64) is very slow! Replace
        // it?
        // BigInteger sn = new BigInteger(new
        // SecureRandom().generateSeed(64));
        BigInteger sn = new BigInteger(64, new Random());
        X500Name owner = new X500Name(certificate.getSubjectDN().getName());
        X500Name issuer = new X500Name(certificate.getIssuerDN().getName());

        info.set(X509CertInfo.VALIDITY, interval);
        info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn));
        info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner));
        info.set(X509CertInfo.ISSUER, new CertificateIssuerName(issuer));
        info.set(X509CertInfo.KEY, new CertificateX509Key(fakedKeyPair.getPublic()));

        info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));

        AlgorithmId algo = new AlgorithmId(new ObjectIdentifier(certificate.getSigAlgOID()));
        info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo));

        // Sign the cert to identify the algorithm that's used.
        fakedCertificate = new X509CertImpl(info);
        fakedCertificate.sign(fakedKeyPair.getPrivate(), certificate.getSigAlgName());
    } catch (CertificateException e) {
        throw new CertificateHandlerException(e);
    } catch (IOException e) {
        throw new CertificateHandlerException(e);
    } catch (InvalidKeyException e) {
        throw new CertificateHandlerException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new CertificateHandlerException(e);
    } catch (NoSuchProviderException e) {
        throw new CertificateHandlerException(e);
    } catch (SignatureException e) {
        throw new CertificateHandlerException(e);
    }
}

From source file:mitm.common.security.certificate.GenerateSSLCertificate.java

private void generateSSLCertificate() throws Exception {
    X509CertificateBuilder certificateBuilder = securityFactory.createX509CertificateBuilder();

    KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA");

    SecureRandom randomSource = securityFactory.createSecureRandom();

    keyPairGenerator.initialize(1024, randomSource);

    KeyPair keyPair = keyPairGenerator.generateKeyPair();

    X500PrincipalBuilder subjectBuilder = new X500PrincipalBuilder();

    subjectBuilder.setCommonName("www.example.com");
    subjectBuilder.setCountryCode("NL");
    subjectBuilder.setLocality("Amsterdam");
    subjectBuilder.setState("NH");

    X500Principal subject = subjectBuilder.buildPrincipal();

    Set<ExtendedKeyUsageType> extendedKeyUsage = new TreeSet<ExtendedKeyUsageType>();

    extendedKeyUsage.add(ExtendedKeyUsageType.CLIENTAUTH);
    extendedKeyUsage.add(ExtendedKeyUsageType.EMAILPROTECTION);
    extendedKeyUsage.add(ExtendedKeyUsageType.SERVERAUTH);

    SerialNumberGenerator sng = new StandardSerialNumberGenerator();

    BigInteger serialNumber = sng.generate();

    Date now = new Date();

    certificateBuilder.setSubject(subject);
    certificateBuilder.setIssuer(subject);
    certificateBuilder.setExtendedKeyUsage(extendedKeyUsage, false);
    certificateBuilder.setNotBefore(DateUtils.addDays(now, -20));
    certificateBuilder.setNotAfter(DateUtils.addYears(now, 20));
    certificateBuilder.setPublicKey(keyPair.getPublic());
    certificateBuilder.setSerialNumber(serialNumber);
    certificateBuilder.setSignatureAlgorithm("SHA1WithRSAEncryption");

    X509Certificate certificate = certificateBuilder.generateCertificate(keyPair.getPrivate(), null);

    assertNotNull(certificate);/*from  w  w w  .ja  v a2s  .  c  o  m*/

    certificates.add(certificate);

    Certificate[] chain = new Certificate[] { certificate };

    keyStore.setKeyEntry("djigzo", keyPair.getPrivate(), null, chain);
}