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:net.nicholaswilliams.java.licensing.encryption.TestKeyFileUtilities.java

@Test
public void testPrivateKeyEncryption04() throws Throwable {
    PrivateKey privateKey = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair()
            .getPrivate();/* w ww  .jav a2s  . c om*/

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

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

    byte[] privateKeyData = KeyFileUtilities.writeEncryptedPrivateKey(privateKey,
            "yourTestPassword02".toCharArray());

    assertNotNull("The key data should not be null.", privateKeyData);
    assertTrue("The key data should have length.", privateKeyData.length > 0);

    PrivateKey privateKey2 = KeyFileUtilities.readEncryptedPrivateKey(privateKeyData,
            "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));
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?/*from  w ww  .  j a  v  a2  s.  c om*/
 *
 * @return Map Map
 * @throws Exception
 */
public static Map<String, String> initKey(UUID licenseCode) throws Exception {
    // ?
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

    // ??
    //        keyPairGen.initialize(KEY_SIZE);
    keyPairGen.initialize(KEY_SIZE, new SecureRandom(licenseCode.toString().getBytes(UTF_8)));

    // ?
    KeyPair keyPair = keyPairGen.generateKeyPair();

    // 
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

    // ?
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

    // ?
    Map<String, String> keyMap = new HashMap<String, String>();

    keyMap.put(PUBLIC_KEY, Base64.encodeBase64String(publicKey.getEncoded()));
    keyMap.put(PRIVATE_KEY, Base64.encodeBase64String(privateKey.getEncoded()));
    keyMap.put(LICENSE_CODE, licenseCode.toString());

    return keyMap;
}

From source file:org.ebayopensource.fido.uaf.crypto.KeyCodec.java

public static KeyPair generate() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    SecureRandom random = new SecureRandom();
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1");
    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA");
    g.initialize(ecSpec, random);//from w ww . ja  va  2  s . com
    return g.generateKeyPair();
}

From source file:org.bedework.util.security.pki.PKITools.java

/**
 * @return RSAKeys//from w  ww.  j a  va 2 s .c om
 * @throws PKIException
 */
public RSAKeys genRSAKeys() throws PKIException {
    RSAKeys keys = new RSAKeys();

    try {
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(new byte[1]);
        KeyPairGenerator rsaKeyGen;

        if (curSchema.pName == null) {
            rsaKeyGen = KeyPairGenerator.getInstance(curSchema.keyFactory);
        } else {
            rsaKeyGen = KeyPairGenerator.getInstance(curSchema.keyFactory, curSchema.pName);
        }

        rsaKeyGen.initialize(1024, secureRandom);

        if (trace()) {
            trace("Generating keys...");
        }

        KeyPair rsaKeyPair = rsaKeyGen.generateKeyPair();

        if (trace()) {
            trace("Saving Public Key...");
        }

        keys.privateKey = rsaKeyPair.getPrivate().getEncoded();
        keys.publicKey = rsaKeyPair.getPublic().getEncoded();

        if (trace()) {
            trace("Done...");
        }

        return keys;
    } catch (Throwable t) {
        throw new PKIException(t);
    }
}

From source file:org.metaeffekt.dcc.agent.AuthenticationKeyGenerator.java

private KeyPair generateKey() throws NoSuchAlgorithmException {
    final KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyGenerator.initialize(DEFAULT_KEY_SIZE);

    return keyGenerator.generateKeyPair();
}

From source file:edu.stanford.mobisocial.dungbeetle.DBIdentityProvider.java

public static KeyPair generateKeyPair() {
    try {//from  w  w  w . j  av  a2  s .  com
        // Generate a 1024-bit Digital Signature Algorithm (RSA) key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        return keyGen.genKeyPair();
    } catch (java.security.NoSuchAlgorithmException e) {
        throw new IllegalStateException("Failed to generate key pair! " + e);
    }
}

From source file:org.apache.nifi.toolkit.tls.util.TlsHelperTest.java

@Before
public void setup() throws Exception {
    days = 360;//www  .  j a v  a  2s.c  o  m
    keySize = 2048;
    keyPairAlgorithm = "RSA";
    signingAlgorithm = "SHA1WITHRSA";
    keyPairGenerator = KeyPairGenerator.getInstance(keyPairAlgorithm);
    keyPairGenerator.initialize(keySize);
    Constructor<KeyStore> keyStoreConstructor = KeyStore.class.getDeclaredConstructor(KeyStoreSpi.class,
            Provider.class, String.class);
    keyStoreConstructor.setAccessible(true);
    keyStore = keyStoreConstructor.newInstance(keyStoreSpi, keyStoreProvider, "faketype");
    keyStore.load(null, null);
    file = File.createTempFile("keystore", "file");
    when(outputStreamFactory.create(file)).thenReturn(tmpFileOutputStream);
}

From source file:org.apache.nifi.toolkit.tls.util.TlsHelper.java

private static KeyPairGenerator createKeyPairGenerator(String algorithm, int keySize)
        throws NoSuchAlgorithmException {
    KeyPairGenerator instance = KeyPairGenerator.getInstance(algorithm);
    instance.initialize(keySize);/*from   www  .j  a  v a 2 s .  com*/
    return instance;
}

From source file:net.jmhertlein.core.crypto.Keys.java

/**
 * Generates a new RSA public/private key pair.
 *
 * The system's default SecureRandom is used
 *
 * @param bits the length of the keys//from  w w  w .  j a v  a2  s. c om
 *
 * @return the new key pair, or null if the RSA algorithm is not supported
 */
public static KeyPair newRSAKeyPair(int bits) {
    KeyPairGenerator keyPairGen;
    try {
        keyPairGen = KeyPairGenerator.getInstance("RSA");
    } catch (NoSuchAlgorithmException ex) {
        return null;
    }

    keyPairGen.initialize(bits);

    return keyPairGen.generateKeyPair();
}

From source file:org.sakaiproject.lti13.LTI13Servlet.java

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    if (ltiService == null) {
        ltiService = (LTIService) ComponentManager.get("org.sakaiproject.lti.api.LTIService");
    }/*ww w  .  j a v a 2 s . co  m*/
    if (tokenKeyPair == null) {
        try {
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            keyGen.initialize(2048);
            tokenKeyPair = keyGen.genKeyPair();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(LTI13Servlet.class.getName()).log(Level.SEVERE, "Unable to generate tokenKeyPair",
                    ex);
        }
    }
}