List of usage examples for java.security KeyPairGenerator generateKeyPair
public KeyPair generateKeyPair()
From source file:cloudeventbus.pki.CertificateUtils.java
public static KeyPair generateKeyPair() { try {//from w w w. j av a2s . c o m final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(KEY_SIZE); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:it.geosolutions.sfs.web.Start.java
private static void assureSelfSignedServerCertificate(String hostname, File keyStoreFile, String password) throws Exception { KeyStore privateKS = KeyStore.getInstance("JKS"); if (keyStoreFile.exists()) { FileInputStream fis = new FileInputStream(keyStoreFile); privateKS.load(fis, password.toCharArray()); if (keyStoreContainsCertificate(privateKS, hostname)) return; } else {/*from w w w. j ava 2 s. co m*/ privateKS.load(null); } // create a RSA key pair generator using 1024 bits KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair KPair = keyPairGenerator.generateKeyPair(); // cerate a X509 certifacte generator // X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator(); // set validity to 10 years, issuer and subject are equal --> self singed certificate int random = new SecureRandom().nextInt(); if (random < 0) random *= -1; // v3CertGen.setSerialNumber(BigInteger.valueOf(random)); // v3CertGen.setIssuerDN(new X509Principal("CN=" + hostname + ", OU=None, O=None L=None, C=None")); // v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30)); // v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10))); // v3CertGen.setSubjectDN(new X509Principal("CN=" + hostname + ", OU=None, O=None L=None, C=None")); // // v3CertGen.setPublicKey(KPair.getPublic()); // v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption"); // // X509Certificate PKCertificate = v3CertGen.generateX509Certificate(KPair.getPrivate()); // // store the certificate containing the public key,this file is needed // to import the public key in other key store. File certFile = new File(keyStoreFile.getParentFile(), hostname + ".cert"); FileOutputStream fos = new FileOutputStream(certFile.getAbsoluteFile()); // fos.write(PKCertificate.getEncoded()); fos.close(); // privateKS.setKeyEntry(hostname+".key", KPair.getPrivate(), // password.toCharArray(), // new java.security.cert.Certificate[]{PKCertificate}); // // privateKS.setCertificateEntry(hostname+".cert",PKCertificate); privateKS.store(new FileOutputStream(keyStoreFile), password.toCharArray()); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?//from w w w. j a v a 2 s. c o m * * @return Map Map */ public static Map<String, Object> initKey() throws Exception { // ? KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); // ?? keyPairGenerator.initialize(KEY_SIZE); // ? KeyPair keyPair = keyPairGenerator.generateKeyPair(); // RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // ? RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // map Map<String, Object> keyMap = new HashMap<String, Object>(); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java
/** * RSA???/*w ww . ja v a 2 s . c om*/ * <dl> * <dt>? * <dd>RSA??????2048?????? * </dl> * @return RSA? */ public static KeyPair createKeyPair() { try { final KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGO_KEY); generator.initialize(2048); final KeyPair pair = generator.generateKeyPair(); if (LOG.isDebugEnabled()) { final RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic(); final RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate(); LOG.debug("public-modulus={}", Base64.encodeBase64String(publicKey.getModulus().toByteArray())); LOG.debug("public-exponent={}", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray())); LOG.debug("private-modulus={}", Base64.encodeBase64String(privateKey.getModulus().toByteArray())); LOG.debug("private-exponent={}", Base64.encodeBase64String(privateKey.getPrivateExponent().toByteArray())); } return pair; } catch (final NoSuchAlgorithmException e) { throw new StandardRuntimeException(e); } }
From source file:org.mitre.jwt.signer.service.impl.KeyStoreTest.java
/** * Create an RSA KeyPair and insert into specified KeyStore * //from w w w . ja v a2s . com * @param location * @param domainName * @param alias * @param keystorePassword * @param aliasPassword * @param daysNotValidBefore * @param daysNotValidAfter * @return * @throws GeneralSecurityException * @throws IOException */ public static java.security.KeyStore generateKeyPair(KeyStore keystore, String keyPairAlgorithm, int keySize, String signatureAlgorithm, String domainName, String alias, String aliasPassword, int daysNotValidBefore, int daysNotValidAfter) throws GeneralSecurityException, IOException { java.security.KeyStore ks; if (keystore != null) { ks = keystore.getKeystore(); } else { ks = java.security.KeyStore.getInstance(java.security.KeyStore.getDefaultType()); ks.load(null, null); } KeyPairGenerator rsaKeyPairGenerator = null; rsaKeyPairGenerator = KeyPairGenerator.getInstance(keyPairAlgorithm); rsaKeyPairGenerator.initialize(keySize); KeyPair rsaKeyPair = rsaKeyPairGenerator.generateKeyPair(); // BC sez X509V3CertificateGenerator is deprecated and the docs say to // use another, but it seemingly isn't included jar... X509V3CertificateGenerator v3CertGen = createCertificate(domainName, daysNotValidBefore, daysNotValidAfter); PrivateKey privateKey = rsaKeyPair.getPrivate(); v3CertGen.setPublicKey(rsaKeyPair.getPublic()); v3CertGen.setSignatureAlgorithm(signatureAlgorithm); // BC docs say to use another, but it seemingly isn't included... X509Certificate certificate = v3CertGen.generateX509Certificate(privateKey); // if exist, overwrite ks.setKeyEntry(alias, privateKey, aliasPassword.toCharArray(), new java.security.cert.Certificate[] { certificate }); if (keystore != null) { keystore.setKeystore(ks); } return ks; }
From source file:net.solarnetwork.node.setup.test.DefaultSetupServiceTest.java
@BeforeClass public static void setupClass() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048, new SecureRandom()); CA_KEY_PAIR = keyGen.generateKeyPair(); CA_CERT = PKITestUtils.generateNewCACert(CA_KEY_PAIR.getPublic(), TEST_CA_DN, null, CA_KEY_PAIR.getPrivate(), TEST_CA_DN); }
From source file:org.javaweb.utils.RSAUtils.java
/** * ?RSA?/*www . j av a 2 s .c o m*/ * * @param keySize * @return * @throws NoSuchAlgorithmException */ public static KeyPair generateKey(int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); keygen.initialize(keySize, random); return keygen.generateKeyPair(); }
From source file:net.solarnetwork.node.setup.test.DefaultKeystoreServiceTest.java
@BeforeClass public static void setupClass() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048, new SecureRandom()); CA_KEY_PAIR = keyGen.generateKeyPair(); CA_CERT = PKITestUtils.generateNewCACert(CA_KEY_PAIR.getPublic(), TEST_CA_DN, null, CA_KEY_PAIR.getPrivate(), TEST_CA_DN); CA_SUB_KEY_PAIR = keyGen.generateKeyPair(); CA_SUB_CERT = PKITestUtils.generateNewCACert(CA_SUB_KEY_PAIR.getPublic(), TEST_CA_SUB_DN, CA_CERT, CA_KEY_PAIR.getPrivate(), TEST_CA_DN); }
From source file:co.cask.cdap.security.tools.KeyStores.java
/** * Create a Java key store with a stored self-signed certificate. * @return Java keystore which has a self signed X.509 certificate */// w w w .j a v a2 s . c o m public static KeyStore generatedCertKeyStore(SConfiguration sConf, String password) { try { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_PAIR_ALGORITHM); SecureRandom random = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM, SECURE_RANDOM_PROVIDER); keyGen.initialize(KEY_SIZE, random); // generate a key pair KeyPair pair = keyGen.generateKeyPair(); int validity = sConf.getInt(Constants.Security.SSL.CERT_VALIDITY, VALIDITY); X509Certificate cert = getCertificate(DISTINGUISHED_NAME, pair, validity, SIGNATURE_ALGORITHM); KeyStore keyStore = KeyStore.getInstance(SSL_KEYSTORE_TYPE); keyStore.load(null, password.toCharArray()); keyStore.setKeyEntry(CERT_ALIAS, pair.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert }); return keyStore; } catch (Exception e) { throw new RuntimeException( "SSL is enabled but a key store file could not be created. A keystore is required " + "for SSL to be used.", e); } }
From source file:com.aaasec.sigserv.cssigapp.KeyStoreFactory.java
/** * Generate a 2048 bit RSA KeyPair.//from ww w .j a v a2s.c o m * * @param algorithm the algorithm to use * @param bits the length of the key (modulus) in bits * * @return the KeyPair * * @exception NoSuchAlgorithmException if no KeyPairGenerator is available * for the requested algorithm */ private static KeyPair generateKeyPair() throws NoSuchAlgorithmException { KeyPair kp = null; KeyPairGenerator generator; generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048); kp = generator.generateKeyPair(); return kp; }