List of usage examples for java.security KeyPairGenerator getInstance
public static KeyPairGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
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:org.wso2.carbon.certificate.mgt.core.util.CSRGenerator.java
/** * Generate the desired keypair/*w ww . j a v a 2 s .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:gemlite.core.util.RSAUtils.java
/** * <p>/*from w ww.j av a2 s . c o m*/ * ?(?) * </p> * * @return * @throws Exception */ public static Map<String, Object> genKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyPairGen.initialize(512); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keyMap = new HashMap<String, Object>(2); keyMap.put(PUBLIC_KEY, publicKey); keyMap.put(PRIVATE_KEY, privateKey); return keyMap; }
From source file:jenkins.security.RSAConfidentialKey.java
/** * Obtains the private key (lazily.)/* www .j a v a2s.com*/ * <p> * This method is not publicly exposed as per the design principle of {@link ConfidentialKey}. * Instead of exposing private key, define methods that use them in specific way, such as * {@link RSADigitalSignatureConfidentialKey}. * * @throws Error * If key cannot be loaded for some reasons, we fail. */ protected synchronized RSAPrivateKey getPrivateKey() { try { if (priv == null) { byte[] payload = load(); if (payload == null) { KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); gen.initialize(2048, new SecureRandom()); // going beyond 2048 requires crypto extension KeyPair keys = gen.generateKeyPair(); priv = (RSAPrivateKey) keys.getPrivate(); pub = (RSAPublicKey) keys.getPublic(); store(priv.getEncoded()); } else { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); priv = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(payload)); RSAPrivateCrtKey pks = (RSAPrivateCrtKey) priv; pub = (RSAPublicKey) keyFactory .generatePublic(new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent())); } } return priv; } catch (IOException e) { throw new Error("Failed to load the key: " + getId(), e); } catch (GeneralSecurityException e) { throw new Error("Failed to load the key: " + getId(), e); } }
From source file:org.candlepin.util.X509CRLEntryStreamTest.java
@Before public void setUp() throws Exception { URL url = X509CRLEntryStreamTest.class.getClassLoader().getResource("crl.der"); derFile = new File(url.getFile()); url = X509CRLEntryStreamTest.class.getClassLoader().getResource("crl.pem"); pemFile = new File(url.getFile()); issuer = new X500Name("CN=Test Issuer"); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048);//from ww w .jav a 2 s .c o m keyPair = generator.generateKeyPair(); signer = new JcaContentSignerBuilder("SHA256WithRSAEncryption").setProvider(BC).build(keyPair.getPrivate()); }
From source file:net.nicholaswilliams.java.licensing.encryption.TestKeyFileUtilities.java
@Test public void testPrivateKeyEncryption01() throws Throwable { File file = new File("testPrivateKeyEncryption01.key"); if (file.exists()) FileUtils.forceDelete(file);/*from ww w .j a v a2s . c om*/ PrivateKey privateKey = KeyPairGenerator.getInstance(KeyFileUtilities.keyAlgorithm).generateKeyPair() .getPrivate(); KeyFileUtilities.writeEncryptedPrivateKey(privateKey, file, "myTestPassword01".toCharArray()); PrivateKey privateKey2 = KeyFileUtilities.readEncryptedPrivateKey(file, "myTestPassword01".toCharArray()); assertNotNull("The key should not be null.", privateKey2); assertFalse("The objects should not be the same.", privateKey == privateKey2); assertEquals("The keys should be the same.", privateKey, privateKey2); FileUtils.forceDelete(file); }
From source file:com.turo.pushy.apns.ApnsClientBenchmark.java
@Setup public void setUp() throws Exception { this.eventLoopGroup = new NioEventLoopGroup(2); final ApnsSigningKey signingKey; {//from w ww. j a v a 2s. com final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC"); final SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); keyPairGenerator.initialize(256, random); signingKey = new ApnsSigningKey(KEY_ID, TEAM_ID, (ECPrivateKey) keyPairGenerator.generateKeyPair().getPrivate()); } final ApnsClientBuilder clientBuilder = new ApnsClientBuilder().setApnsServer(HOST, PORT) .setConcurrentConnections(this.concurrentConnections).setSigningKey(signingKey) .setTrustedServerCertificateChain( ApnsClientBenchmark.class.getResourceAsStream(CA_CERTIFICATE_FILENAME)) .setEventLoopGroup(this.eventLoopGroup); this.client = clientBuilder.build(); this.server = new BenchmarkApnsServer( ApnsClientBenchmark.class.getResourceAsStream(SERVER_CERTIFICATES_FILENAME), ApnsClientBenchmark.class.getResourceAsStream(SERVER_KEY_FILENAME), this.eventLoopGroup); final String token = generateRandomToken(); this.pushNotifications = new ArrayList<>(this.notificationCount); final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder(); for (int i = 0; i < this.notificationCount; i++) { final String payload = payloadBuilder .setAlertBody(RandomStringUtils.randomAlphanumeric(MESSAGE_BODY_LENGTH)) .buildWithDefaultMaximumLength(); this.pushNotifications.add(new SimpleApnsPushNotification(token, TOPIC, payload)); } this.server.start(PORT).await(); }
From source file:org.demosoft.medieval.life.loginserver.LoginController.java
private LoginController() throws GeneralSecurityException { _log.info("Loading LoginContoller..."); _keyPairs = new ScrambledKeyPair[10]; KeyPairGenerator keygen = null; keygen = KeyPairGenerator.getInstance("RSA"); RSAKeyGenParameterSpec spec = new RSAKeyGenParameterSpec(1024, RSAKeyGenParameterSpec.F4); keygen.initialize(spec);// ww w.ja va 2s.co m // generate the initial set of keys for (int i = 0; i < 10; i++) { _keyPairs[i] = new ScrambledKeyPair(keygen.generateKeyPair()); } _log.info("Cached 10 KeyPairs for RSA communication"); testCipher((RSAPrivateKey) _keyPairs[0]._pair.getPrivate()); // Store keys for blowfish communication generateBlowFishKeys(); }
From source file:mitm.common.security.ca.handlers.comodo.ApplyCustomClientCertTest.java
private KeyPair generateKeyPair() throws NoSuchAlgorithmException { return KeyPairGenerator.getInstance("RSA").generateKeyPair(); }
From source file:com.leclercb.commons.api.license.LicenseManager.java
public static KeyPair generateKeys(String algorithm, int keySize) throws NoSuchAlgorithmException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm); keyGen.initialize(keySize);//from ww w. ja v a2s . c o m return keyGen.genKeyPair(); }