List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
From source file:org.cesecore.certificates.util.AlgorithmTools.java
/** Check if the curve name is known by the first found PKCS#11 provider or default (if none was found)*/ public static boolean isNamedECKnownInDefaultProvider(String ecNamedCurveBc) { final Provider[] providers = Security.getProviders("KeyPairGenerator.EC"); String providerName = providers[0].getName(); try {//from w w w .java 2 s . co m for (Provider ecProvider : providers) { //This will list something like: SunPKCS11-NSS, BC, SunPKCS11-<library>-slot<slotnumber> if (log.isDebugEnabled()) { log.debug("Found EC capable provider named: " + ecProvider.getName()); } if (ecProvider.getName().startsWith("SunPKCS11-") && !ecProvider.getName().startsWith("SunPKCS11-NSS")) { providerName = ecProvider.getName(); break; } } final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", providerName); kpg.initialize(new ECGenParameterSpec(ecNamedCurveBc)); return true; } catch (InvalidAlgorithmParameterException e) { if (log.isDebugEnabled()) { log.debug(ecNamedCurveBc + " is not available in provider " + providerName); } } catch (NoSuchAlgorithmException e) { throw new RuntimeException( "EC capable provider " + providerName + " could no longer handle elliptic curve algorithm..", e); } catch (NoSuchProviderException e) { throw new RuntimeException("EC capable provider " + providerName + " disappeard unexpectedly.", e); } return false; }
From source file:de.pawlidi.openaletheia.utils.CipherUtils.java
/** * //from w w w. ja v a 2s . co m * @return */ public static KeyPair generateKeyPair() { KeyPairGenerator generator = null; SecureRandom secureRandom = null; try { generator = KeyPairGenerator.getInstance(CIPHER_ALGORITHM); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Could not generate key", e); } try { secureRandom = SecureRandom.getInstance(RANDOM_NUMBER_GENERATOR_ALGORITHM, "SUN"); } catch (NoSuchAlgorithmException | NoSuchProviderException e) { // ignore exception } if (secureRandom == null) { generator.initialize(2048); } else { generator.initialize(2048, secureRandom); } return generator.generateKeyPair(); }
From source file:org.globus.gsi.util.CertificateUtil.java
/** * Generates a key pair of given algorithm and strength. * * @param algorithm the algorithm of the key pair. * @param bits the strength/* ww w.j a va 2s.c o m*/ * @return <code>KeyPair</code> the generated key pair. * @exception GeneralSecurityException if something goes wrong. */ public static KeyPair generateKeyPair(String algorithm, int bits) throws GeneralSecurityException { KeyPairGenerator generator = null; if (provider == null) { generator = KeyPairGenerator.getInstance(algorithm); } else { generator = KeyPairGenerator.getInstance(algorithm, provider); } generator.initialize(bits); return generator.generateKeyPair(); }
From source file:cl.nic.dte.util.XMLUtil.java
public static AUTORIZACIONDocument generateAuthorization(AUTORIZACIONDocument template, PrivateKey pKey) throws NoSuchAlgorithmException, SignatureException, TransformerException, InvalidKeyException, IOException {/* w w w . j a v a2 s .c o m*/ // Generation of keys KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024); KeyPair kp = kpg.generateKeyPair(); CAFType caf = template.getAUTORIZACION().getCAF(); CAFType.DA.RSAPK rsapk = caf.getDA().addNewRSAPK(); rsapk.setM(((RSAPublicKey) kp.getPublic()).getModulus().toByteArray()); rsapk.setE(((RSAPublicKey) kp.getPublic()).getPublicExponent().toByteArray()); ResourceBundle labels = ResourceBundle.getBundle("cl.nic.dte.resources.VerifyResults"); Signature sig = null; if (pKey.getAlgorithm().equals("RSA")) { sig = Signature.getInstance("SHA1withRSA"); caf.addNewFRMA().setAlgoritmo("SHA1withRSA"); } else if (pKey.getAlgorithm().equals("DSA")) { sig = Signature.getInstance("SHA1withDSA"); caf.addNewFRMA().setAlgoritmo("SHA1withDSA"); } else { throw new NoSuchAlgorithmException( labels.getString("ALGORITHM_NOT_SUPPORTED").replaceAll("%1", pKey.getAlgorithm())); } template.getAUTORIZACION() .setRSASK("-----BEGIN RSA PRIVATE KEY-----\n" + new String(Base64.encodeBase64(kp.getPrivate().getEncoded(), true)) + "-----END RSA PRIVATE KEY-----\n"); template.getAUTORIZACION() .setRSAPUBK("-----BEGIN RSA PUBLIC KEY-----\n" + new String(Base64.encodeBase64(kp.getPublic().getEncoded(), true)) + "-----END RSA PUBLIC KEY-----\n"); sig.initSign(pKey); sig.update(XMLUtil.getCleaned(caf.getDA())); caf.getFRMA().setByteArrayValue(Base64.encodeBase64(sig.sign())); return template; }
From source file:com.trsst.Common.java
static final KeyPair generateSigningKeyPair() { try {//from w w w . j av a 2 s.com KeyPairGenerator kpg; // kpg = KeyPairGenerator.getInstance("EC", "BC"); kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC(); kpg.initialize(new ECGenParameterSpec(CURVE_NAME)); KeyPair kp = kpg.generateKeyPair(); return kp; // } catch (NoSuchAlgorithmException e) { // log.error("Error while generating key: " + e.getMessage(), e); // } catch (NoSuchProviderException e) { // log.error("Error while generating key: " + e.getMessage(), e); } catch (InvalidAlgorithmParameterException e) { log.error("Error while generating key: " + e.getMessage(), e); } return null; }
From source file:com.trsst.Common.java
static final KeyPair generateEncryptionKeyPair() { try {/*from ww w . j a v a 2s . c o m*/ KeyPairGenerator kpg; // kpg = KeyPairGenerator.getInstance("EC", "BC"); kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC(); kpg.initialize(new ECGenParameterSpec(CURVE_NAME)); KeyPair kp = kpg.generateKeyPair(); return kp; // } catch (NoSuchAlgorithmException e) { // log.error("Error while generating key: " + e.getMessage(), e); // } catch (NoSuchProviderException e) { // e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } return null; }
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); return keyGen.genKeyPair(); }
From source file:bftsmart.tom.util.RSAKeyPairGenerator.java
/** * Generate the key pair for the process with id = <id> and put it on the * files config/keys/publickey<id> and config/keys/privatekey<id> * * @param id the id of the process to generate key * @throws Exception something goes wrong when writing the files *///www .jav a 2 s .c om public void run(int id, int size) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(size); KeyPair kp = keyGen.generateKeyPair(); PublicKey puk = kp.getPublic(); PrivateKey prk = kp.getPrivate(); saveToFile(id, puk, prk); }
From source file:cherry.foundation.crypto.RSASignatureSupportTest.java
private RSASignatureSupport createCrypto() throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048); KeyPair key = keygen.generateKeyPair(); RSASignatureSupport crypto = new RSASignatureSupport(); crypto.setAlgorithm("SHA256withRSA"); crypto.setPublicKeyResource(new InMemoryResource(key.getPublic().getEncoded())); crypto.setPrivateKeyResource(new InMemoryResource(key.getPrivate().getEncoded())); crypto.afterPropertiesSet();/* w w w.j a v a 2 s . c om*/ return crypto; }
From source file:cherry.foundation.crypto.RSACryptoSupportTest.java
private RSACryptoSupport createCrypto() throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048); KeyPair key = keygen.generateKeyPair(); RSACryptoSupport crypto = new RSACryptoSupport(); crypto.setAlgorithm("RSA/ECB/PKCS1Padding"); crypto.setPublicKeyResource(new InMemoryResource(key.getPublic().getEncoded())); crypto.setPrivateKeyResource(new InMemoryResource(key.getPrivate().getEncoded())); crypto.afterPropertiesSet();//from w ww .j a v a 2 s . c om return crypto; }