List of usage examples for java.security KeyPairGenerator getInstance
public static KeyPairGenerator getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
From source file:edu.vt.middleware.crypt.CryptProvider.java
/** * <p>This finds a <code>KeyPairGenerator</code> using the known providers and * the supplied algorithm parameter.</p> * * @param algorithm <code>String</code> name * * @return <code>KeyPairGenerator</code> * * @throws CryptException if the algorithm is not available from any * provider or if the provider is not available in the environment *//* w w w .ja v a 2 s .c o m*/ public static KeyPairGenerator getKeyPairGenerator(final String algorithm) throws CryptException { final Log logger = LogFactory.getLog(CryptProvider.class); KeyPairGenerator generator = null; for (int i = 0; i < providers.length; i++) { try { generator = KeyPairGenerator.getInstance(algorithm, providers[i]); } catch (NoSuchAlgorithmException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find algorithm " + algorithm + " in " + providers[i]); } } catch (NoSuchProviderException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find provider " + providers[i]); } } finally { if (generator != null) { break; } } } if (generator == null) { try { generator = KeyPairGenerator.getInstance(algorithm); } catch (NoSuchAlgorithmException e) { if (logger.isDebugEnabled()) { logger.debug("Could not find algorithm " + algorithm); } throw new CryptException(e.getMessage()); } } return generator; }
From source file:org.cesecore.keys.util.KeyStoreTools.java
private void generateExtraEC(final String name, final String keyEntryName, final String algInstanceName, final String sigAlgName) throws InvalidAlgorithmParameterException { if (log.isTraceEnabled()) { log.trace(">generate " + algInstanceName + ": curve name " + name + ", keyEntryName " + keyEntryName); }// w w w . j a v a 2s . co m // Generate the EC Keypair KeyPairGenerator kpg; try { kpg = KeyPairGenerator.getInstance(algInstanceName, this.providerName); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm " + name + "was not recognized.", e); } catch (NoSuchProviderException e) { throw new IllegalStateException("BouncyCastle was not found as a provider.", e); } try { ECGenParameterSpec ecSpec = new ECGenParameterSpec(name); kpg.initialize(ecSpec); } catch (InvalidAlgorithmParameterException e) { log.debug("EC " + algInstanceName + " name " + name + " not supported."); throw e; } generateKeyPair(kpg, keyEntryName, sigAlgName); if (log.isTraceEnabled()) { log.trace("<generate: curve name " + name + ", keyEntryName " + keyEntryName); } }
From source file:org.ejbca.util.keystore.KeyStoreContainerBase.java
@Override public byte[] generate(final AlgorithmParameterSpec spec, final String keyEntryName) throws Exception { if (log.isTraceEnabled()) { log.trace(">generate from AlgorithmParameterSpec: " + spec.getClass().getName()); }/* w w w. j a va 2 s. c o m*/ // Generate the Keypair String algorithm = "EC"; String sigAlg = "SHA1withECDSA"; String specName = spec.getClass().getName(); if (specName.contains("DSA")) { algorithm = "DSA"; sigAlg = "SHA1withDSA"; } else if (specName.contains("RSA")) { algorithm = "RSA"; sigAlg = "SHA1withRSA"; } final KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm, this.providerName); try { kpg.initialize(spec); } catch (InvalidAlgorithmParameterException e) { log.debug("Algorithm parameters not supported: " + e.getMessage()); throw e; } final byte result[] = generate(kpg, keyEntryName, sigAlg); if (log.isTraceEnabled()) { log.trace("<generate from AlgorithmParameterSpec: " + spec.getClass().getName()); } return result; }
From source file:com.thoughtworks.go.security.X509CertificateGenerator.java
private KeyPair generateKeyPair() { try {/* ww w. j a va 2s .com*/ return KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair(); } catch (Exception e) { throw bomb("Couldn't create public-private key pair", e); } }
From source file:org.kse.gui.dialogs.sign.DSignJar.java
public static void main(String[] args) throws Exception { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); Security.addProvider(new BouncyCastleProvider()); java.awt.EventQueue.invokeLater(new Runnable() { @Override// w w w. ja v a 2 s.c om public void run() { try { KeyPairGenerator kpg = KeyPairGenerator.getInstance(KeyPairType.RSA.jce(), "BC"); kpg.initialize(1024, new SecureRandom()); KeyPair kp = kpg.generateKeyPair(); DSignJar dialog = new DSignJar(new JFrame(), kp.getPrivate(), KeyPairType.RSA, "signature name", null); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { System.exit(0); } @Override public void windowDeactivated(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); }
From source file:org.cesecore.keys.util.KeyStoreTools.java
private void generateRSA(final int keySize, final String keyEntryName) { if (log.isTraceEnabled()) { log.trace(">generate: keySize " + keySize + ", keyEntryName " + keyEntryName); }// w w w . j av a 2s .co m // Generate the RSA Keypair KeyPairGenerator kpg; try { kpg = KeyPairGenerator.getInstance("RSA", this.providerName); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Algorithm " + "RSA" + "was not recognized.", e); } catch (NoSuchProviderException e) { throw new IllegalStateException("BouncyCastle was not found as a provider.", e); } kpg.initialize(keySize); generateKeyPair(kpg, keyEntryName, "SHA1withRSA"); if (log.isTraceEnabled()) { log.trace("<generate: keySize " + keySize + ", keyEntryName " + keyEntryName); } }
From source file:nl.afas.cordova.plugin.secureLocalStorage.SecureLocalStorage.java
@TargetApi(18) private KeyStore initKeyStore() throws SecureLocalStorageException { try {//from w ww . jav a 2s . c o m KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); if (!keyStore.containsAlias(SECURELOCALSTORAGEALIAS)) { Calendar start = Calendar.getInstance(); Calendar end = Calendar.getInstance(); end.add(Calendar.YEAR, 3); KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(_cordova.getActivity()) .setAlias(SECURELOCALSTORAGEALIAS) .setSubject(new X500Principal(String.format("CN=%s, O=%s", "SecureLocalStorage", _cordova.getActivity().getBaseContext().getPackageName()))) .setSerialNumber(BigInteger.ONE).setStartDate(start.getTime()).setEndDate(end.getTime()) .build(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore"); generator.initialize(spec); generator.generateKeyPair(); } return keyStore; } catch (Exception e) { throw new SecureLocalStorageException("Could not initialize keyStore", e); } }
From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java
public static KeyPair generateRandomKeyPair() throws NoSuchProviderException, NoSuchAlgorithmException { Security.addProvider(new BouncyCastleProvider()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); keyPairGenerator.initialize(2048, new SecureRandom()); return keyPairGenerator.generateKeyPair(); }
From source file:com.eucalyptus.crypto.DefaultCryptoProvider.java
/** * @see com.eucalyptus.crypto.CertificateProvider#generateKeyPair() *///from ww w . j av a 2s.c o m @Override public KeyPair generateKeyPair() { KeyPairGenerator keyGen = null; try { EventRecord.caller(DefaultCryptoProvider.class, EventType.GENERATE_KEYPAIR); keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM, PROVIDER); SecureRandom random = Crypto.getSecureRandomSupplier().get(); //TODO: RELEASE: see line:110 keyGen.initialize(KEY_SIZE, random); KeyPair keyPair = keyGen.generateKeyPair(); return keyPair; } catch (Exception e) { LOG.fatal(e, e); return null; } }
From source file:org.signserver.server.cryptotokens.KeystoreCryptoTokenTest.java
/** * Tests that a worker just set up with a key store containing a new * key-pair and is activated manually gets status ACTIVE. * @throws Exception/*from w ww . j a v a 2s .com*/ */ public void testActivateWithNewKeystore() throws Exception { LOG.info("testActivateWithNewKeystore"); final boolean autoActivate = false; final int workerId = WORKER_CMS; try { setCMSSignerPropertiesCombined(workerId, autoActivate); // Create a key-pair and certificate in the keystore FileOutputStream out = null; try { KeyStore ks = KeyStore.getInstance("PKCS12", "BC"); ks.load(null, null); // Generate key and issue certificate final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC"); kpg.initialize(1024); final KeyPair keyPair = kpg.generateKeyPair(); X509Certificate[] chain = new X509Certificate[1]; chain[0] = getSelfCertificate("CN=TestActivateWithNewKeystore" + ", C=SE", (long) 30 * 24 * 60 * 60 * 365, keyPair); ks.setKeyEntry("newkey11", keyPair.getPrivate(), pin.toCharArray(), chain); out = new FileOutputStream(keystoreFile); ks.store(out, pin.toCharArray()); } finally { IOUtils.closeQuietly(out); } workerSession.setWorkerProperty(workerId, "DEFAULTKEY", "newkey11"); workerSession.reloadConfiguration(workerId); // Activate first so we can generate a key workerSession.activateSigner(workerId, pin); List<String> errors = workerSession.getStatus(workerId).getFatalErrors(); assertTrue("Fatal errors: " + errors, workerSession.getStatus(workerId).getFatalErrors().isEmpty()); } finally { FileUtils.deleteQuietly(keystoreFile); removeWorker(workerId); } }