List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException
From source file:org.wso2.carbon.certificate.mgt.core.util.CSRGenerator.java
/** * Generate the desired keypair/*from w ww . j ava 2s. co m*/ * * @param alg * @param keySize * @return */ public KeyPair generateKeyPair(String alg, int keySize) { try { KeyPairGenerator keyPairGenerator = null; keyPairGenerator = KeyPairGenerator.getInstance(alg); keyPairGenerator.initialize(keySize); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { log.error("The provided algorithm is not found ", e); } return null; }
From source file:org.candlepin.CRLWriteBenchmark.java
@Setup(Level.Trial) public void createKey() throws Exception { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); keyPair = generator.generateKeyPair(); }
From source file:org.candlepin.CRLBenchmark.java
@Setup(Level.Trial) public void buildMassiveCRL() throws Exception { X500Name issuer = new X500Name("CN=Test Issuer"); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); KeyPair keyPair = generator.generateKeyPair(); Provider bc = new BouncyCastleProvider(); ContentSigner signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(bc) .build(keyPair.getPrivate()); X509v2CRLBuilder crlBuilder = new X509v2CRLBuilder(issuer, new Date()); crlBuilder.addExtension(X509Extension.authorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(keyPair.getPublic())); /* With a CRL number of 127, incrementing it should cause the number of bytes in the length * portion of the TLV to increase by one.*/ crlBuilder.addExtension(X509Extension.cRLNumber, false, new CRLNumber(new BigInteger("127"))); for (int i = 0; i < 2000000; i++) { crlBuilder.addCRLEntry(new BigInteger(String.valueOf(i)), new Date(), CRLReason.unspecified); }// www . j a v a 2 s. co m X509CRLHolder holder = crlBuilder.build(signer); X509CRL crl = new JcaX509CRLConverter().setProvider(bc).getCRL(holder); crlFile = File.createTempFile("crl", ".der"); System.out.println("\nWrote test crl to " + crlFile.getAbsolutePath()); FileUtils.writeByteArrayToFile(crlFile, crl.getEncoded()); }
From source file:org.globus.gsi.bc.BouncyCastleOpenSSLKeyTest.java
private KeyPair getKeyPair() throws Exception { CertUtil.init();/* ww w. j a v a 2 s . c o 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 {//ww w .j av a 2 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:de.rub.nds.burp.utilities.attacks.signatureFaking.helper.CertificateHandler.java
public void createFakedCertificate() throws CertificateHandlerException { try {/*from w w w . ja va 2 s . c o m*/ Logging.getInstance().log(getClass(), "Faking the found certificate", Logging.DEBUG); KeyPairGenerator kpg = KeyPairGenerator.getInstance(originalPublicKey.getAlgorithm()); kpg.initialize(((RSAPublicKey) certificate.getPublicKey()).getModulus().bitLength()); fakedKeyPair = kpg.generateKeyPair(); X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator(); v3CertGen.setSubjectDN(certificate.getSubjectX500Principal()); v3CertGen.setIssuerDN(certificate.getIssuerX500Principal()); v3CertGen.setNotAfter(certificate.getNotAfter()); v3CertGen.setNotBefore(certificate.getNotBefore()); v3CertGen.setSerialNumber(new BigInteger(64, new Random())); v3CertGen.setSignatureAlgorithm(certificate.getSigAlgName()); v3CertGen.setPublicKey(fakedKeyPair.getPublic()); fakedCertificate = v3CertGen.generate(fakedKeyPair.getPrivate()); } catch (CertificateEncodingException | SecurityException | SignatureException | InvalidKeyException | NoSuchAlgorithmException e) { throw new CertificateHandlerException(e); } }
From source file:org.candlepin.CRLWriteBenchmark.java
@Setup(Level.Trial) public void buildMassiveCRL() throws Exception { issuer = new X500Name("CN=Test Issuer"); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); KeyPair keyPair = generator.generateKeyPair(); bc = new BouncyCastleProvider(); signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(bc).build(keyPair.getPrivate()); X509v2CRLBuilder crlBuilder = new X509v2CRLBuilder(issuer, new Date()); crlBuilder.addExtension(X509Extension.authorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(keyPair.getPublic())); /* With a CRL number of 127, incrementing it should cause the number of bytes in the length * portion of the TLV to increase by one.*/ crlBuilder.addExtension(X509Extension.cRLNumber, false, new CRLNumber(new BigInteger("127"))); for (int i = 0; i < 2000000; i++) { crlBuilder.addCRLEntry(new BigInteger(String.valueOf(i)), new Date(), CRLReason.unspecified); }/* w w w. j av a2 s. c om*/ X509CRLHolder holder = crlBuilder.build(signer); X509CRL crl = new JcaX509CRLConverter().setProvider(bc).getCRL(holder); crlFile = File.createTempFile("crl", ".der"); System.out.println("\nWrote test crl to " + crlFile.getAbsolutePath()); FileUtils.writeByteArrayToFile(crlFile, crl.getEncoded()); }
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); 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);//from www . ja v a 2s .c om 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.sshd.common.config.keys.loader.pem.PKCS8PEMResourceKeyPairParserTest.java
@Test // see SSHD-760 public void testPkcs8() throws IOException, GeneralSecurityException { KeyPairGenerator generator = SecurityUtils.getKeyPairGenerator(algorithm); if (keySize > 0) { generator.initialize(keySize); }//from w w w . j a va 2 s .c om KeyPair kp = generator.generateKeyPair(); try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { Collection<Object> items = new ArrayList<>(); PrivateKey prv1 = kp.getPrivate(); items.add(new PEMItem(prv1.getEncoded(), "PRIVATE KEY")); byte[] bytes = PEMUtil.encode(items); os.write(bytes); os.close(); try (ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { KeyPair kp2 = SecurityUtils.loadKeyPairIdentity(getCurrentTestName(), bais, null); assertEquals("Mismatched public key", kp.getPublic(), kp2.getPublic()); assertEquals("Mismatched private key", prv1, kp2.getPrivate()); } } }
From source file:org.apache.abdera2.common.security.DHBase.java
private void init(BigInteger p, BigInteger g, int l, byte[] key) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException { DHParameterSpec spec = new DHParameterSpec(p, g, l); KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("DH"); keypairgen.initialize(spec); keyPair = keypairgen.generateKeyPair(); publicKey = decode(key);//from ww w .j av a 2s . co m }