List of usage examples for java.security KeyPairGenerator initialize
public void initialize(AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException
From source file:org.xdi.oxauth.model.crypto.signature.RSAKeyFactory.java
public RSAKeyFactory(SignatureAlgorithm signatureAlgorithm, String dnName) throws InvalidParameterException, NoSuchProviderException, NoSuchAlgorithmException, SignatureException, InvalidKeyException, CertificateEncodingException { if (signatureAlgorithm == null) { throw new InvalidParameterException("The signature algorithm cannot be null"); }// www . j av a 2 s .c om KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC"); keyGen.initialize(2048, new SecureRandom()); KeyPair keyPair = keyGen.generateKeyPair(); JCERSAPrivateCrtKey jcersaPrivateCrtKey = (JCERSAPrivateCrtKey) keyPair.getPrivate(); JCERSAPublicKey jcersaPublicKey = (JCERSAPublicKey) keyPair.getPublic(); rsaPrivateKey = new RSAPrivateKey(jcersaPrivateCrtKey.getModulus(), jcersaPrivateCrtKey.getPrivateExponent()); rsaPublicKey = new RSAPublicKey(jcersaPublicKey.getModulus(), jcersaPublicKey.getPublicExponent()); if (StringUtils.isNotBlank(dnName)) { // Create certificate GregorianCalendar startDate = new GregorianCalendar(); // time from which certificate is valid GregorianCalendar expiryDate = new GregorianCalendar(); // time after which certificate is not valid expiryDate.add(Calendar.YEAR, 1); BigInteger serialNumber = new BigInteger(1024, new Random()); // serial number for certificate X509V1CertificateGenerator certGen = new X509V1CertificateGenerator(); X500Principal principal = new X500Principal(dnName); certGen.setSerialNumber(serialNumber); certGen.setIssuerDN(principal); certGen.setNotBefore(startDate.getTime()); certGen.setNotAfter(expiryDate.getTime()); certGen.setSubjectDN(principal); // note: same as issuer certGen.setPublicKey(keyPair.getPublic()); certGen.setSignatureAlgorithm(signatureAlgorithm.getAlgorithm()); X509Certificate x509Certificate = certGen.generate(jcersaPrivateCrtKey, "BC"); certificate = new Certificate(signatureAlgorithm, x509Certificate); } }
From source file:net.maritimecloud.identityregistry.utils.CertificateUtil.java
/** * Generates a keypair (public and private) based on Elliptic curves. * /*from ww w.j a v a 2s. c o m*/ * @return The generated keypair */ public static KeyPair generateKeyPair() { ECGenParameterSpec ecGenSpec = new ECGenParameterSpec("secp384r1"); KeyPairGenerator g; try { g = KeyPairGenerator.getInstance("ECDSA", BC_PROVIDER_NAME); } catch (NoSuchAlgorithmException | NoSuchProviderException e) { throw new RuntimeException(e.getMessage(), e); } try { g.initialize(ecGenSpec, new SecureRandom()); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e.getMessage(), e); } KeyPair pair = g.generateKeyPair(); return pair; }
From source file:com.awstrainers.devcourse.sdkdemos.S3Test.java
@Test public void checkUploadEncryptedObject() throws Exception { // Check the next url to read about how to use strong encryption with the jdk. // http://www.oracle.com/technetwork/es/java/javase/downloads/jce-7-download-432124.html KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.genKeyPair(); // obviously that keypair should be stored somewhere, but this is just a test. EncryptionMaterials encryptionMaterials = new EncryptionMaterials(keyPair); AmazonS3 client = new AmazonS3EncryptionClient(cred, encryptionMaterials); log.info(format("Using {0} bucket to post a new encrypted file.", bucketName)); File file = createTempFile(); Bucket bucket = client.createBucket(bucketName); PutObjectResult result = client.putObject(bucket.getName(), file.getName(), file); }
From source file:mitm.common.security.ca.SMIMEKeyAndCertificateIssuer.java
private synchronized KeyPair generateKeyPair(RequestParameters parameters) throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA"); keyPairGenerator.initialize(parameters.getKeyLength(), randomSource); return keyPairGenerator.generateKeyPair(); }
From source file:test.unit.org.owasp.webscarab.util.SunCertificateUtilsTest.java
private KeyPair generateKeyPair(int size) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); keyPairGenerator.initialize(new RSAKeyGenParameterSpec(size, RSAKeyGenParameterSpec.F4), random); KeyPair keyPair = keyPairGenerator.generateKeyPair(); return keyPair; }
From source file:mitm.common.tools.CreateCA.java
private void generateIntermediate() throws Exception { X509CertificateBuilder certificateBuilder = securityFactory.createX509CertificateBuilder(); KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA"); keyPairGenerator.initialize(2048, randomSource); intermediateKeyPair = keyPairGenerator.generateKeyPair(); X500PrincipalBuilder issuerBuilder = new X500PrincipalBuilder(); String email = "martijn@mitm.nl"; issuerBuilder.setCommonName("MITM Intermediate"); issuerBuilder.setCountryCode("NL"); issuerBuilder.setEmail(email);//from ww w . j a v a2 s. co m issuerBuilder.setLocality("Amsterdam"); issuerBuilder.setState("NH"); AltNamesBuilder altNamesBuider = new AltNamesBuilder(); altNamesBuider.setRFC822Names(email); X500Principal issuer = issuerBuilder.buildPrincipal(); GeneralNames altNames = altNamesBuider.buildAltNames(); Date now = new Date(); certificateBuilder.setSubject(issuer); certificateBuilder.setIssuer(rootCertificate.getSubjectX500Principal()); certificateBuilder.setAltNames(altNames, true); certificateBuilder.setNotBefore(now); certificateBuilder.setNotAfter(DateUtils.addYears(now, 20)); certificateBuilder.setPublicKey(intermediateKeyPair.getPublic()); certificateBuilder.setSerialNumber(serialNumberGenerator.generate()); certificateBuilder.setSignatureAlgorithm("SHA1WithRSAEncryption"); certificateBuilder.setIsCA(true, true); certificateBuilder.addSubjectKeyIdentifier(true); intermediateCertificate = certificateBuilder.generateCertificate(rootKeyPair.getPrivate(), intermediateCertificate); }
From source file:mitm.common.tools.CreateCA.java
private void generateRoot() throws Exception { X509CertificateBuilder certificateBuilder = securityFactory.createX509CertificateBuilder(); KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA"); keyPairGenerator.initialize(2048, randomSource); rootKeyPair = keyPairGenerator.generateKeyPair(); X500PrincipalBuilder issuerBuilder = new X500PrincipalBuilder(); String email = "martijn@mitm.nl"; issuerBuilder.setCommonName("MITM Root"); issuerBuilder.setCountryCode("NL"); issuerBuilder.setEmail(email);/*from w w w. jav a 2 s . c o m*/ issuerBuilder.setLocality("Amsterdam"); issuerBuilder.setState("NH"); AltNamesBuilder altNamesBuider = new AltNamesBuilder(); altNamesBuider.setRFC822Names(email); X500Principal issuer = issuerBuilder.buildPrincipal(); GeneralNames altNames = altNamesBuider.buildAltNames(); Date now = new Date(); certificateBuilder.setSubject(issuer); certificateBuilder.setIssuer(issuer); certificateBuilder.setAltNames(altNames, true); certificateBuilder.setNotBefore(now); certificateBuilder.setNotAfter(DateUtils.addYears(now, 20)); certificateBuilder.setPublicKey(rootKeyPair.getPublic()); certificateBuilder.setSerialNumber(serialNumberGenerator.generate()); certificateBuilder.setSignatureAlgorithm("SHA1WithRSAEncryption"); certificateBuilder.setIsCA(true, true); certificateBuilder.addSubjectKeyIdentifier(true); /* * generate self signed certificate */ rootCertificate = certificateBuilder.generateCertificate(rootKeyPair.getPrivate(), null); }
From source file:com.streamsets.lib.security.util.DataSignature.java
public KeyPair generateKeyPair() throws GeneralSecurityException { KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA"); SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN"); rng.setSeed(System.currentTimeMillis()); keyGenerator.initialize(1024, rng); return keyGenerator.generateKeyPair(); }
From source file:org.gluu.com.ox_push2.u2f.v2.cert.KeyPairGeneratorImpl.java
@Override public KeyPair generateKeyPair() throws U2FException { // generate ECC key SecureRandom random = new SecureRandom(); ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256r1"); try {// w w w. jav a2 s . c o m KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", bouncyCastleProvider); g.initialize(ecSpec, random); KeyPair keyPair = g.generateKeyPair(); return keyPair; } catch (NoSuchAlgorithmException ex) { throw new U2FException("Failed to generate key pair", ex); } catch (InvalidAlgorithmParameterException ex) { throw new U2FException("Failed to generate key pair", ex); } }
From source file:mitm.common.security.ca.SMIMECABuilder.java
private KeyPair generateKeyPair(int keyLength) throws NoSuchAlgorithmException, NoSuchProviderException { KeyPairGenerator keyPairGenerator = securityFactory.createKeyPairGenerator("RSA"); keyPairGenerator.initialize(keyLength, randomSource); return keyPairGenerator.generateKeyPair(); }