Example usage for java.security KeyPairGenerator initialize

List of usage examples for java.security KeyPairGenerator initialize

Introduction

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

Prototype

public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException 

Source Link

Document

Initializes the key pair generator using the specified parameter set and the SecureRandom implementation of the highest-priority installed provider as the source of randomness.

Usage

From source file:cn.util.RSAUtils.java

/**
 * ??/*  w  w w.  ja  v a2  s.  c o  m*/
 * @throws NoSuchAlgorithmException 
 *
 */
public static HashMap<String, Object> getKeys() throws NoSuchAlgorithmException {
    HashMap<String, Object> map = new HashMap<String, Object>();
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(1024);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    map.put("public", publicKey);
    map.put("private", privateKey);
    return map;
}

From source file:org.panbox.core.crypto.CryptCore.java

public static KeyPair generateKeypair() {
    try {/*ww  w . j av a2  s .  co m*/
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(KeyConstants.KEY_FACTORY, KeyConstants.PROV_BC);
        kpg.initialize(KeyConstants.ASYMMETRIC_KEYSIZE);
        KeyPair kp = kpg.generateKeyPair();
        return kp;
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error during asymmetric key pair generation: " + e);
    } catch (NoSuchProviderException e) {
        logger.error("Error during asymmetric key pair generation: " + e);
    }
    return null;
}

From source file:org.umit.icm.mobile.utils.RSACrypto.java

public static KeyPair generateKey() throws Exception {

    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
    keyPairGen.initialize(Constants.RSA_KEY_SIZE);
    return keyPairGen.generateKeyPair();
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateNewKeyOld(Context context) {
    try {/*w  ww.j ava  2  s  . c  om*/
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
                KEY_PROVIDER);

        Calendar instance = Calendar.getInstance();
        Date start = instance.getTime();

        instance.add(Calendar.YEAR, 1);
        Date end = instance.getTime();

        keyPairGenerator.initialize(new KeyPairGeneratorSpec.Builder(context).setAlias(KEY_ALIAS)
                .setSubject(new X500Principal("CN=" + KEY_ALIAS)).setSerialNumber(BigInteger.valueOf(20151021))
                .setStartDate(start).setEndDate(end).build());

        keyPairGenerator.generateKeyPair();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java

public static KeyPair generateKey() throws GeneralSecurityException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
    keyGen.initialize(1024);
    KeyPair key = keyGen.generateKeyPair();
    return key;/*from ww w . j  av  a2 s  .  c  om*/
}

From source file:info.magnolia.cms.security.SecurityUtil.java

public static MgnlKeyPair generateKeyPair(int keyLength) throws NoSuchAlgorithmException {
    KeyPairGenerator kgen = KeyPairGenerator.getInstance(ALGORITHM);
    kgen.initialize(keyLength);
    KeyPair key = kgen.genKeyPair();
    return new MgnlKeyPair(byteArrayToHex(key.getPrivate().getEncoded()),
            byteArrayToHex(key.getPublic().getEncoded()));
}

From source file:gemlite.core.util.RSAUtils.java

/**
 * <p>/*w w w . ja v  a2 s .c o  m*/
 * ?(?)
 * </p>
 * 
 * @return
 * @throws Exception
 */
public static Map<String, Object> genKeyPair() throws Exception {
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    keyPairGen.initialize(512);
    KeyPair keyPair = keyPairGen.generateKeyPair();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    Map<String, Object> keyMap = new HashMap<String, Object>(2);
    keyMap.put(PUBLIC_KEY, publicKey);
    keyMap.put(PRIVATE_KEY, privateKey);
    return keyMap;
}

From source file:netinf.common.security.impl.CryptographyTest.java

@BeforeClass
public static void classSetUp() throws Exception {
    final Properties properties = Utils.loadProperties(NETINFNODE_PROPERTIES);
    injector = Guice.createInjector(new LogModule(properties), new DatamodelImplModule(),
            new CommunicationModule(), new SecurityModule(), new AbstractModule() {

                @Override//from w w  w .j  a  v  a 2s.c om
                protected void configure() {
                    bind(NetInfNodeConnection.class).annotatedWith(SecurityModule.Security.class)
                            .to(RemoteNodeConnection.class).in(Singleton.class);
                    Names.bindProperties(binder(), properties);
                }
            });
    factory = injector.getInstance(DatamodelFactory.class);

    identityObject = factory.createIdentityObject();
    Identifier id = factory.createIdentifier();
    IdentifierLabel label = factory.createIdentifierLabel();
    label.setLabelName(DefinedLabelName.UNIQUE_LABEL.getLabelName());
    label.setLabelValue("Test-Identity");
    id.addIdentifierLabel(label);
    identityObject.setIdentifier(id);

    try {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        KeyPair pair = keyPairGenerator.generateKeyPair();

        privateKey = pair.getPrivate();
        publicKey = pair.getPublic();
        String keyName = identityObject.getIdentifier().toString() + "?"
                + DefinedAttributeIdentification.PUBLIC_KEY.getURI();

        publicKeys.put(keyName, publicKey);

        identityObject.setPublicMasterKey(pair.getPublic());
    } catch (Exception e) {
        throw new NetInfUncheckedException("error creating keys");

    }
    convenienceCommunicator = EasyMock.createMock(RemoteNodeConnection.class);
    convenienceCommunicator.setHostAndPort("localhost", 5000);
    EasyMock.expectLastCall().anyTimes();
    convenienceCommunicator.setSerializeFormat(SerializeFormat.JAVA);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.expect(convenienceCommunicator.getIO((Identifier) EasyMock.anyObject())).andReturn(identityObject)
            .anyTimes();
    EasyMock.replay(convenienceCommunicator);

    identityManager = EasyMock.createMock(IdentityManager.class);
    EasyMock.expect(identityManager.getPrivateKey((String) EasyMock.anyObject())).andReturn(privateKey)
            .anyTimes();
    EasyMock.expect(identityManager.hasPrivateKey((String) EasyMock.anyObject())).andReturn(true).anyTimes();
    EasyMock.expect(identityManager.getPrivateKey(((String) EasyMock.anyObject()),
            (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(privateKey).anyTimes();
    EasyMock.expect(identityManager.hasPrivateKey(((String) EasyMock.anyObject()),
            (String) EasyMock.anyObject(), (String) EasyMock.anyObject())).andReturn(true).anyTimes();
    EasyMock.replay(identityManager);

    crypto = new CryptographyImpl(identityManager, algorithm, factory, convenienceCommunicator);
}

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/*  www  .  ja  va 2  s.  co  m*/
 * @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: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 www .  ja  v a2 s. c o  m
 *
 * @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();
}