Example usage for java.security KeyPairGenerator genKeyPair

List of usage examples for java.security KeyPairGenerator genKeyPair

Introduction

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

Prototype

public final KeyPair genKeyPair() 

Source Link

Document

Generates a key pair.

Usage

From source file:com.vmware.demo.SamlUtils.java

/**
 * Generate a RSA key pair (default size SamlUtils.keySize specified as 2048 bits)
 *
 * @return the new KeyPair/*from w w w  .  ja v a  2s  .  c om*/
 * @throws SamlException
 */
public static KeyPair generateKey(int keySize) throws SamlException {
    KeyPair pair = null;

    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(keySize);
        pair = keyGen.genKeyPair();
    } catch (Exception e) {
        throw new SamlException("Failed to generate RSA signing key.", e);
    }

    return pair;
}

From source file:com.streamsets.datacollector.credential.cyberark.TestWebServicesFetcher.java

private static KeyPair generateKeyPair(String algorithm) throws NoSuchAlgorithmException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
    keyGen.initialize(1024);/*from  ww w .j  av a  2s .  co m*/
    return keyGen.genKeyPair();
}

From source file:com.ibm.mobilefirstplatform.clientsdk.android.security.mca.internal.security.DefaultJSONSignerTests.java

public KeyPair generateRandomKeyPair() {
    KeyPair keyPair = null;//from ww w.  j a  v a 2 s .  c  o m

    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(512);
        keyPair = kpg.genKeyPair();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    return keyPair;
}

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);/*  w ww  .  j  a  v  a 2  s  . c  om*/

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

    return Pair.of(publicKey, privateKey);
}

From source file:org.globus.gsi.bc.BouncyCastleOpenSSLKeyTest.java

private KeyPair getKeyPair() throws Exception {
    CertUtil.init();/*from www.  j a v a 2  s .  co m*/

    int bits = 512;

    KeyPairGenerator keyGen = null;
    keyGen = KeyPairGenerator.getInstance("RSA", "BC");
    keyGen.initialize(bits);

    return keyGen.genKeyPair();
}

From source file:de.dennishoersch.web.chat.Encryption.java

public KeyPair generateRSAKeyPair() {
    try {/*from  www  .j  a  v  a2 s .c  o  m*/
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        return keyGen.genKeyPair();

    } catch (NoSuchAlgorithmException e) {
        throw Throwables.throwUnchecked(e);
    }
}

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);/*from w w w  .j a  v  a  2s .  co 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:org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions.java

@Override
public void init() throws MalformedURLException, GeneralSecurityException {
    Security.addProvider(new BouncyCastleProvider());
    KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEY_ALGORITHM, "BC");
    kpg.initialize(KEY_SIZE);// ww  w  .  ja va 2  s .  c  o m
    caKeyPair = kpg.genKeyPair();

    X500NameBuilder subjectBuilder = new X500NameBuilder(BCStyle.INSTANCE);
    subjectBuilder.addRDN(BCStyle.CN, "RootCA");

    try {
        sigGen = new JcaContentSignerBuilder(SIGNATURE_ALGORITHM).setProvider("BC")
                .build(caKeyPair.getPrivate());
        X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(subjectBuilder.build(),
                BigInteger.ONE, new Date(), new Date(System.currentTimeMillis() + 600000),
                subjectBuilder.build(), caKeyPair.getPublic());
        caCert = new JcaX509CertificateConverter().setProvider("BC").getCertificate(certGen.build(sigGen));

        caCert.checkValidity();
        caCert.verify(caKeyPair.getPublic());
        caCert.verify(caCert.getPublicKey());
    } catch (OperatorCreationException ex) {
        throw new GeneralSecurityException(ex);
    }
}

From source file:org.kde.kdeconnect.NetworkPackageTest.java

public void testEncryption() throws JSONException {
    NetworkPackage original = new NetworkPackage("com.test");
    original.set("hello", "hola");

    NetworkPackage copy = NetworkPackage.unserialize(original.serialize());

    NetworkPackage decrypted = new NetworkPackage("");

    KeyPair keyPair;/*from  www.j ava 2s .co m*/
    try {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048);
        keyPair = keyGen.genKeyPair();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("KDE/initializeRsaKeys", "Exception");
        return;
    }

    PrivateKey privateKey = keyPair.getPrivate();
    assertNotNull(privateKey);

    PublicKey publicKey = keyPair.getPublic();
    assertNotNull(publicKey);

    // Encrypt and decrypt np
    assertEquals(original.getType(), "com.test");
    try {
        NetworkPackage encrypted = RsaHelper.encrypt(original, publicKey);
        assertEquals(encrypted.getType(), NetworkPackage.PACKAGE_TYPE_ENCRYPTED);

        decrypted = RsaHelper.decrypt(encrypted, privateKey);
        assertEquals(decrypted.getType(), "com.test");

    } catch (Exception e) {
        e.printStackTrace();
    }

    // np should be equal to np2
    assertEquals(decrypted.getLong("id"), copy.getLong("id"));
    assertEquals(decrypted.getType(), copy.getType());
    assertEquals(decrypted.getJSONArray("body"), copy.getJSONArray("body"));

    String json = "{\"body\":{\"nowPlaying\":\"A really long song name - A really long artist name\",\"player\":\"A really long player name\",\"the_meaning_of_life_the_universe_and_everything\":\"42\"},\"id\":945945945,\"type\":\"kdeconnect.a_really_really_long_package_type\"}\n";
    NetworkPackage longJsonNp = NetworkPackage.unserialize(json);
    try {
        NetworkPackage encrypted = RsaHelper.encrypt(longJsonNp, publicKey);
        decrypted = RsaHelper.decrypt(encrypted, privateKey);
        String decryptedJson = decrypted.serialize();
        JSONAssert.assertEquals(json, decryptedJson, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.awstrainers.devcourse.sdkdemos.S3Test.java

@Test
public void checkUploadEncryptedObject() throws Exception {
    // Check the next url to read about how to use strong encryption with the jdk.
    // http://www.oracle.com/technetwork/es/java/javase/downloads/jce-7-download-432124.html
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(1024, new SecureRandom());
    KeyPair keyPair = keyPairGenerator.genKeyPair();
    // obviously that keypair should be stored somewhere, but this is just a test.
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(keyPair);
    AmazonS3 client = new AmazonS3EncryptionClient(cred, encryptionMaterials);
    log.info(format("Using {0} bucket to post a new encrypted file.", bucketName));
    File file = createTempFile();
    Bucket bucket = client.createBucket(bucketName);
    PutObjectResult result = client.putObject(bucket.getName(), file.getName(), file);
}