@Test public void createSelfSignedCertificate() { Crypto.createSelfSignedCertificate(new File("target/test.jks"), "default", "secret"); }
public static void createSelfSignedCertificate(File keystore, String alias, String keystorePassword) { try { Provider bouncyCastleProvider = new BouncyCastleProvider(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", bouncyCastleProvider); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); nameBuilder.addRDN(BCStyle.CN, "localhost"); X500Name name = nameBuilder.build(); Date notBefore = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date notAfter = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(name, serial, notBefore, notAfter, name, keyPair.getPublic()); ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption") .setProvider(bouncyCastleProvider).build(keyPair.getPrivate()); X509Certificate cert = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider) .getCertificate(certGen.build(sigGen)); cert.checkValidity(new Date()); cert.verify(cert.getPublicKey()); KeyStore store = KeyStore.getInstance("JKS"); if (keystore.exists()) { try (FileInputStream fis = new FileInputStream(keystore)) { store.load(fis, keystorePassword.toCharArray()); } } else { store.load(null); } store.setKeyEntry(alias, keyPair.getPrivate(), keystorePassword.toCharArray(), new Certificate[] { cert }); try (FileOutputStream fos = new FileOutputStream(keystore)) { store.store(fos, keystorePassword.toCharArray()); } } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | InvalidKeyException | NoSuchProviderException | SignatureException | KeyStoreException | IOException e) { throw new RuntimeException(e); } }
Crypto { public static void createSelfSignedCertificate(File keystore, String alias, String keystorePassword) { try { Provider bouncyCastleProvider = new BouncyCastleProvider(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", bouncyCastleProvider); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); nameBuilder.addRDN(BCStyle.CN, "localhost"); X500Name name = nameBuilder.build(); Date notBefore = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date notAfter = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(name, serial, notBefore, notAfter, name, keyPair.getPublic()); ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption") .setProvider(bouncyCastleProvider).build(keyPair.getPrivate()); X509Certificate cert = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider) .getCertificate(certGen.build(sigGen)); cert.checkValidity(new Date()); cert.verify(cert.getPublicKey()); KeyStore store = KeyStore.getInstance("JKS"); if (keystore.exists()) { try (FileInputStream fis = new FileInputStream(keystore)) { store.load(fis, keystorePassword.toCharArray()); } } else { store.load(null); } store.setKeyEntry(alias, keyPair.getPrivate(), keystorePassword.toCharArray(), new Certificate[] { cert }); try (FileOutputStream fos = new FileOutputStream(keystore)) { store.store(fos, keystorePassword.toCharArray()); } } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | InvalidKeyException | NoSuchProviderException | SignatureException | KeyStoreException | IOException e) { throw new RuntimeException(e); } } }
Crypto { public static void createSelfSignedCertificate(File keystore, String alias, String keystorePassword) { try { Provider bouncyCastleProvider = new BouncyCastleProvider(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", bouncyCastleProvider); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); nameBuilder.addRDN(BCStyle.CN, "localhost"); X500Name name = nameBuilder.build(); Date notBefore = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date notAfter = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(name, serial, notBefore, notAfter, name, keyPair.getPublic()); ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption") .setProvider(bouncyCastleProvider).build(keyPair.getPrivate()); X509Certificate cert = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider) .getCertificate(certGen.build(sigGen)); cert.checkValidity(new Date()); cert.verify(cert.getPublicKey()); KeyStore store = KeyStore.getInstance("JKS"); if (keystore.exists()) { try (FileInputStream fis = new FileInputStream(keystore)) { store.load(fis, keystorePassword.toCharArray()); } } else { store.load(null); } store.setKeyEntry(alias, keyPair.getPrivate(), keystorePassword.toCharArray(), new Certificate[] { cert }); try (FileOutputStream fos = new FileOutputStream(keystore)) { store.store(fos, keystorePassword.toCharArray()); } } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | InvalidKeyException | NoSuchProviderException | SignatureException | KeyStoreException | IOException e) { throw new RuntimeException(e); } } private  Crypto(); }
Crypto { public static void createSelfSignedCertificate(File keystore, String alias, String keystorePassword) { try { Provider bouncyCastleProvider = new BouncyCastleProvider(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", bouncyCastleProvider); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); nameBuilder.addRDN(BCStyle.CN, "localhost"); X500Name name = nameBuilder.build(); Date notBefore = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date notAfter = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(name, serial, notBefore, notAfter, name, keyPair.getPublic()); ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption") .setProvider(bouncyCastleProvider).build(keyPair.getPrivate()); X509Certificate cert = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider) .getCertificate(certGen.build(sigGen)); cert.checkValidity(new Date()); cert.verify(cert.getPublicKey()); KeyStore store = KeyStore.getInstance("JKS"); if (keystore.exists()) { try (FileInputStream fis = new FileInputStream(keystore)) { store.load(fis, keystorePassword.toCharArray()); } } else { store.load(null); } store.setKeyEntry(alias, keyPair.getPrivate(), keystorePassword.toCharArray(), new Certificate[] { cert }); try (FileOutputStream fos = new FileOutputStream(keystore)) { store.store(fos, keystorePassword.toCharArray()); } } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | InvalidKeyException | NoSuchProviderException | SignatureException | KeyStoreException | IOException e) { throw new RuntimeException(e); } } private  Crypto(); static void createSelfSignedCertificate(File keystore, String alias,
												   String keystorePassword); static SSLContext createNoValidationSSLContext(); }
Crypto { public static void createSelfSignedCertificate(File keystore, String alias, String keystorePassword) { try { Provider bouncyCastleProvider = new BouncyCastleProvider(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", bouncyCastleProvider); keyPairGenerator.initialize(1024, new SecureRandom()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); X500NameBuilder nameBuilder = new X500NameBuilder(BCStyle.INSTANCE); nameBuilder.addRDN(BCStyle.CN, "localhost"); X500Name name = nameBuilder.build(); Date notBefore = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000); Date notAfter = new Date(System.currentTimeMillis() + 10 * 365 * 24 * 60 * 60 * 1000); BigInteger serial = BigInteger.valueOf(System.currentTimeMillis()); X509v3CertificateBuilder certGen = new JcaX509v3CertificateBuilder(name, serial, notBefore, notAfter, name, keyPair.getPublic()); ContentSigner sigGen = new JcaContentSignerBuilder("SHA256WithRSAEncryption") .setProvider(bouncyCastleProvider).build(keyPair.getPrivate()); X509Certificate cert = new JcaX509CertificateConverter().setProvider(bouncyCastleProvider) .getCertificate(certGen.build(sigGen)); cert.checkValidity(new Date()); cert.verify(cert.getPublicKey()); KeyStore store = KeyStore.getInstance("JKS"); if (keystore.exists()) { try (FileInputStream fis = new FileInputStream(keystore)) { store.load(fis, keystorePassword.toCharArray()); } } else { store.load(null); } store.setKeyEntry(alias, keyPair.getPrivate(), keystorePassword.toCharArray(), new Certificate[] { cert }); try (FileOutputStream fos = new FileOutputStream(keystore)) { store.store(fos, keystorePassword.toCharArray()); } } catch (NoSuchAlgorithmException | OperatorCreationException | CertificateException | InvalidKeyException | NoSuchProviderException | SignatureException | KeyStoreException | IOException e) { throw new RuntimeException(e); } } private  Crypto(); static void createSelfSignedCertificate(File keystore, String alias,
												   String keystorePassword); static SSLContext createNoValidationSSLContext();  }