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.util.JwtUtil.java
public static KeyPair generateKeyES384() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-384"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC"); keyGen.initialize(ecSpec, new SecureRandom()); return keyGen.generateKeyPair(); }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
public static KeyPair generateKeyES512() throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException { ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-521"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC"); keyGen.initialize(ecSpec, new SecureRandom()); return keyGen.generateKeyPair(); }
From source file:info.fcrp.keepitsafe.bean.UserBeanTest.java
private String generatePublicKey() throws NoSuchAlgorithmException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024, new SecureRandom()); KeyPair kp = kpg.generateKeyPair(); PublicKey pubKey = kp.getPublic(); return Base64.encodeBase64String(pubKey.getEncoded()); }
From source file:MainClass.java
public KeyPair generateKeyPair(long seed) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("DSA"); SecureRandom rng = SecureRandom.getInstance("SHA1PRNG", "SUN"); rng.setSeed(seed);//w w w . j ava 2 s . co m keyGenerator.initialize(1024, rng); return (keyGenerator.generateKeyPair()); }
From source file:com.vmware.admiral.auth.lightwave.pc.X509CertificateHelper.java
public X509Certificate generateX509Certificate(String keyPairAlg, String sigAlg) throws NoSuchAlgorithmException, CertificateException, OperatorCreationException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyPairAlg); keyGen.initialize(1024, new SecureRandom()); KeyPair clientKeyPair = keyGen.generateKeyPair(); String solutionUser = "oidc.client" + UUID.randomUUID().toString(); return generateCertificate(clientKeyPair, solutionUser, sigAlg); }
From source file:jef.tools.security.EncrypterUtil.java
/** * ?KEY//from w w w.j a v a2 s. c o m * * @param algom * ? DSA RSA * @return */ public static final KeyPair generateKeyPair(String algom) { try { java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator.getInstance(algom); SecureRandom secrand = new SecureRandom(); secrand.setSeed("\n".getBytes()); // ?? // 512 1024 64 ? keygen.initialize(1024, secrand); // ?? // keygen.initialize(512); KeyPair keys = keygen.generateKeyPair(); // ? return keys; } catch (GeneralSecurityException e) { throw new RuntimeException(e); } }
From source file:net.solarnetwork.pki.bc.test.BCCertificateServiceTest.java
@Test public void signCertificate() throws Exception { X509Certificate cert = service.generateCertificate(TEST_DN, publicKey, privateKey); String csr = service.generatePKCS10CertificateRequestString(cert, privateKey); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048, new SecureRandom()); KeyPair caKeypair = keyGen.generateKeyPair(); X509Certificate caCert = service.generateCertificationAuthorityCertificate(TEST_CA_DN, caKeypair.getPublic(), caKeypair.getPrivate()); X509Certificate signed = service.signCertificate(csr, caCert, caKeypair.getPrivate()); assertEquals("Issuer", caCert.getSubjectX500Principal(), signed.getIssuerX500Principal()); assertEquals("Subject", cert.getSubjectX500Principal(), signed.getSubjectX500Principal()); }
From source file:info.fcrp.keepitsafe.bean.CryptBeanTest.java
@Test public void assymetric() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); kpg.initialize(1024, new SecureRandom()); KeyPair kp = kpg.generateKeyPair(); PrivateKey priKey = kp.getPrivate(); PublicKey pubKey = kp.getPublic(); Cipher c = Cipher.getInstance("RSA"); String plain = "plain"; byte[] plainBytes = plain.getBytes(); c.init(Cipher.ENCRYPT_MODE, pubKey); c.update(plainBytes);//from w w w . j a v a 2 s. c om byte[] encBytes = c.doFinal(); String enc = Base64.encodeBase64String(encBytes); assertNotSame(plain, enc); c.init(Cipher.DECRYPT_MODE, priKey); c.update(encBytes); byte[] decBytes = c.doFinal(); String dec = new String(decBytes); assertEquals(plain, dec); }
From source file:jenkins.security.RSAConfidentialKey.java
/** * Obtains the private key (lazily.)/*from w w w . j a va 2 s . co m*/ * <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.xdi.oxauth.model.crypto.signature.ECDSAKeyFactory.java
public ECDSAKeyFactory(SignatureAlgorithm signatureAlgorithm, String dnName) throws InvalidParameterException, NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, SignatureException, InvalidKeyException, CertificateEncodingException { if (signatureAlgorithm == null) { throw new InvalidParameterException("The signature algorithm cannot be null"); }//from www . j a va2s .c o m ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec(signatureAlgorithm.getCurve()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC"); keyGen.initialize(ecSpec, new SecureRandom()); KeyPair keyPair = keyGen.generateKeyPair(); JCEECPrivateKey privateKeySpec = (JCEECPrivateKey) keyPair.getPrivate(); JCEECPublicKey publicKeySpec = (JCEECPublicKey) keyPair.getPublic(); BigInteger x = publicKeySpec.getQ().getX().toBigInteger(); BigInteger y = publicKeySpec.getQ().getY().toBigInteger(); BigInteger d = privateKeySpec.getD(); ecdsaPrivateKey = new ECDSAPrivateKey(d); ecdsaPublicKey = new ECDSAPublicKey(signatureAlgorithm, x, y); 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("SHA256WITHECDSA"); X509Certificate x509Certificate = certGen.generate(privateKeySpec, "BC"); certificate = new Certificate(signatureAlgorithm, x509Certificate); } }