public X509Certificate generateCertificate(String subjectDN, String issuerDN, KeyPair pair, PrivateKey privkey, int days, String signingAlgorithm) throws CertificateException
    {
        if (privkey == null)
        {
            privkey = pair.getPrivate();
        }
        X500Name issuer;
        if (issuerDN == null)
        {
            issuer = new X500Name(RFC4519Style.INSTANCE, subjectDN);
        }
        else
        {
            issuer = new X500Name(RFC4519Style.INSTANCE, issuerDN);
        }
        long now = System.currentTimeMillis();
        BigInteger serial = BigInteger.probablePrime(64, new SecureRandom(Primitives.writeLong(now)));
        X500Name subject = new X500Name(RFC4519Style.INSTANCE, subjectDN);
        PublicKey publicKey = pair.getPublic();
        byte[] encoded = publicKey.getEncoded();
        SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo.getInstance(encoded);
        X509v3CertificateBuilder builder = new X509v3CertificateBuilder(
                issuer,
                serial,
                new Date(now - 86400000l),
                new Date(now + days * 86400000l),
                subject,
                subjectPublicKeyInfo
        );
        X509CertificateHolder holder = builder.build(createSigner(privkey, signingAlgorithm));
        return new JcaX509CertificateConverter().getCertificate(holder);
    }
public X509Certificate generateCertificate ( String subjectDN , String issuerDN , KeyPair pair , PrivateKey privkey , int days , String signingAlgorithm ) throws CertificateException { if ( privkey == null ) { privkey = pair . getPrivate ( ) ; } X500Name issuer ; if ( issuerDN == null ) { issuer = new X500Name ( RFC4519Style . INSTANCE , subjectDN ) ; } else { issuer = new X500Name ( RFC4519Style . INSTANCE , issuerDN ) ; } long now = System . currentTimeMillis ( ) ; BigInteger serial = BigInteger . probablePrime ( 64 , new SecureRandom ( Primitives . writeLong ( now ) ) ) ; X500Name subject = new X500Name ( RFC4519Style . INSTANCE , subjectDN ) ; PublicKey publicKey = pair . getPublic ( ) ; byte [ ] encoded = publicKey . getEncoded ( ) ; SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfo . getInstance ( encoded ) ; X509v3CertificateBuilder builder = new X509v3CertificateBuilder ( issuer , serial , new Date ( now - 86400000l ) , new Date ( now + days * 86400000l ) , subject , subjectPublicKeyInfo ) ; X509CertificateHolder holder = builder . build ( createSigner ( privkey , signingAlgorithm ) ) ; return new JcaX509CertificateConverter ( ) . getCertificate ( holder ) ; }
Create a signed X.509 Certificate
@param subjectDN the X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
@param issuerDN Signers X.509 Distinguished Name, eg "CN=Test, L=London, C=GB"
@param pair the KeyPair
@param privkey Signers private key
@param days how many days from now the Certificate is valid for
@param signingAlgorithm the signing algorithm, e.g. "SHA1withRSA"
@return
@throws java.security.cert.CertificateException