Example usage for java.security KeyPairGenerator getInstance

List of usage examples for java.security KeyPairGenerator getInstance

Introduction

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

Prototype

public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a KeyPairGenerator object that generates public/private key pairs for the specified algorithm.

Usage

From source file:com.microsoft.azure.management.TestContainerService.java

private String getSshKey() throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(2048);//from  w  w w. j a v  a2  s  . com
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(byteOs);
    dos.writeInt("ssh-rsa".getBytes().length);
    dos.write("ssh-rsa".getBytes());
    dos.writeInt(publicKey.getPublicExponent().toByteArray().length);
    dos.write(publicKey.getPublicExponent().toByteArray());
    dos.writeInt(publicKey.getModulus().toByteArray().length);
    dos.write(publicKey.getModulus().toByteArray());
    String publicKeyEncoded = new String(Base64.encodeBase64(byteOs.toByteArray()));
    return "ssh-rsa " + publicKeyEncoded + " ";
}

From source file:org.candlepin.util.X509CRLStreamWriterTest.java

@Before
public void setUp() throws Exception {
    issuer = new X500Name("CN=Test Issuer");

    generator = KeyPairGenerator.getInstance("RSA");
    generator.initialize(2048);/*from  w ww  . j a va 2s  .c om*/
    keyPair = generator.generateKeyPair();

    signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(BC).build(keyPair.getPrivate());

    outfile = new File(folder.getRoot(), "new.crl");
    Security.addProvider(BC);
}

From source file:org.artifactory.security.crypto.CryptoHelper.java

public static KeyPair generateKeyPair() {
    try {/*from  ww w .  ja va 2  s .  c o  m*/
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ASYM_ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        keyGen.initialize(512, random);
        return keyGen.generateKeyPair();
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("No such algorithm:" + e.getMessage());
    }
}

From source file:org.openbaton.nfvo.core.api.KeyManagement.java

@Override
public String generateKey(String projectId, String name) throws IOException, NoSuchAlgorithmException {
    log.debug("Generating keypair");
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(2048);/*  w w  w  .  j a  va  2s.  c o  m*/
    KeyPair keyPair = keyGen.genKeyPair();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    String publicKeyString = encodePublicKey(publicKey, name);
    Key key = new Key();
    key.setName(name);
    key.setProjectId(projectId);
    key.setFingerprint(calculateFingerprint(publicKey.getEncoded()));
    key.setPublicKey(publicKeyString);
    log.debug(publicKeyString);
    keyRepository.save(key);
    log.info("Added new key: " + key);
    return parsePrivateKey(privateKey.getEncoded());
}

From source file:com.formkiq.core.service.crypto.SecureTokenServiceImpl.java

@Override
public Pair<RSAPublicKey, RSAPrivateKey> initializeToken(final int keysize) throws NoSuchAlgorithmException {

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(keysize);/*from w  w  w .j av a  2  s.c o m*/

    KeyPair kp = kpg.genKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate();

    return Pair.of(publicKey, privateKey);
}

From source file:com.kuzumeji.platform.standard.SecurityService.java

/**
 * RSA?(?/?)??//  w ww  . ja  v  a  2s .  c  o m
 * <dl>
 * <dt>?
 * <dd>RSA?(?/?)??(?=2048)
 * </dl>
 * @return ?
 */
public KeyPair generateKeyPair() {
    try {
        final KeyPairGenerator keygen = KeyPairGenerator.getInstance(RSA_ALGO_NAME);
        keygen.initialize(KEYSIZE, SECURE_RANDOM);
        return keygen.generateKeyPair();
    } catch (final NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.abdera2.common.security.DHBase.java

private void init() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidParameterSpecException, InvalidKeySpecException {
    AlgorithmParameterGenerator pgen = AlgorithmParameterGenerator.getInstance("DH");
    pgen.init(512);/*  w w w .j a va 2  s.  c o  m*/
    AlgorithmParameters params = pgen.generateParameters();
    DHParameterSpec dhspec = (DHParameterSpec) params.getParameterSpec(DHParameterSpec.class);
    KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("DH");
    keypairgen.initialize(dhspec);
    keyPair = keypairgen.generateKeyPair();
    p = dhspec.getP();
    g = dhspec.getG();
    l = dhspec.getL();
}

From source file:org.javaweb.utils.RSAUtils.java

/**
 * ?RSA?/*w  w w  .  j  a va  2 s.c o m*/
 *
 * @param keySize
 * @return
 * @throws NoSuchAlgorithmException
 */
public static KeyPair generateKey(int keySize) throws NoSuchAlgorithmException {
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    SecureRandom random = new SecureRandom();
    keygen.initialize(keySize, random);

    return keygen.generateKeyPair();
}

From source file:net.nicholaswilliams.java.licensing.encryption.TestKeyFileUtilities.java

@Test
public void testPrivateKeyEncryption02() throws Throwable {
    File file = new File("testPrivateKeyEncryption02.key");

    if (file.exists())
        FileUtils.forceDelete(file);/*from w w  w  .ja  va2  s. c om*/

    PrivateKey privateKey = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair()
            .getPrivate();

    PrivateKey otherKey = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair()
            .getPrivate();

    assertFalse("The keys should not be equal (1).", otherKey.equals(privateKey));

    KeyFileUtilities.writeEncryptedPrivateKey(privateKey, file, "yourTestPassword02".toCharArray());

    PrivateKey privateKey2 = KeyFileUtilities.readEncryptedPrivateKey(file, "yourTestPassword02".toCharArray());

    assertNotNull("The key should not be null.", privateKey2);
    assertFalse("The objects should not be the same.", privateKey == privateKey2);
    assertEquals("The keys should be the same.", privateKey, privateKey2);

    assertFalse("The keys should not be equal (2).", otherKey.equals(privateKey2));

    FileUtils.forceDelete(file);
}