Here you can find the source of generateRSAKeypairAndKeystore(String fullyQualifiedDN, Date endDate, String keystoreLocation, String keyPairAlias, String keypairPassword, String keystorePassword)
public static KeyStore generateRSAKeypairAndKeystore(String fullyQualifiedDN, Date endDate, String keystoreLocation, String keyPairAlias, String keypairPassword, String keystorePassword) throws IOException, GeneralSecurityException
//package com.java2s; //License from project: Creative Commons License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.math.BigInteger; import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.KeyStore; import java.security.PrivateKey; import java.security.SecureRandom; import java.security.cert.Certificate; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.Date; import sun.security.x509.AlgorithmId; import sun.security.x509.CertificateAlgorithmId; import sun.security.x509.CertificateIssuerName; import sun.security.x509.CertificateSerialNumber; import sun.security.x509.CertificateSubjectName; import sun.security.x509.CertificateValidity; import sun.security.x509.CertificateVersion; import sun.security.x509.CertificateX509Key; import sun.security.x509.X500Name; import sun.security.x509.X509CertImpl; import sun.security.x509.X509CertInfo; public class Main { public static KeyStore generateRSAKeypairAndKeystore(String fullyQualifiedDN, Date endDate, String keystoreLocation, String keyPairAlias, String keypairPassword, String keystorePassword) throws IOException, GeneralSecurityException { KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, null);// w w w. j a va2s . com KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); keyGen.initialize(2048, random); KeyPair keyPair = keyGen.generateKeyPair(); X509Certificate cert = generateX509Certificate(fullyQualifiedDN, keyPair, endDate, "SHA1withRSA"); keyStore.setKeyEntry(keyPairAlias, keyPair.getPrivate(), keypairPassword.toCharArray(), new Certificate[] { cert }); File keyFile = new File(keystoreLocation); FileOutputStream outputStream = new FileOutputStream(keyFile); try { keyStore.store(outputStream, keystorePassword.toCharArray()); } finally { try { outputStream.close(); } catch (IOException e) { } } return keyStore; } public static X509Certificate generateX509Certificate(String dn, KeyPair pair, Date to, String algorithm) throws GeneralSecurityException, IOException { PrivateKey privkey = pair.getPrivate(); X509CertInfo info = new X509CertInfo(); Date from = new Date(); CertificateValidity interval = new CertificateValidity(from, to); BigInteger sn = new BigInteger(64, SecureRandom.getInstance("SHA1PRNG")); X500Name owner = new X500Name(dn); info.set(X509CertInfo.VALIDITY, interval); info.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber(sn)); try { info.set(X509CertInfo.SUBJECT, new CertificateSubjectName(owner)); info.set(X509CertInfo.ISSUER, new CertificateIssuerName(owner)); } catch (CertificateException e) { info.set(X509CertInfo.SUBJECT, owner); info.set(X509CertInfo.ISSUER, owner); } info.set(X509CertInfo.KEY, new CertificateX509Key(pair.getPublic())); info.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3)); //info.set(X509CertInfo.DN_NAME, dn); //AlgorithmId algo = new AlgorithmId(AlgorithmId.DH_oid); AlgorithmId algo = new AlgorithmId(AlgorithmId.sha256WithRSAEncryption_oid); info.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(algo)); // Sign the cert to identify the algorithm that's used. X509CertImpl cert = new X509CertImpl(info); cert.sign(privkey, algorithm); // Update the algorith, and resign. algo = (AlgorithmId) cert.get(X509CertImpl.SIG_ALG); info.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algo); cert = new X509CertImpl(info); cert.sign(privkey, algorithm); return cert; } }