}
//Get PrivateKey
RSAPrivateCrtKey jksPrivateCrtKey = null;
try {
jksPrivateCrtKey =
(RSAPrivateCrtKey) jksKeyStore.getKey(jksAlias, jksPassword);
System.out.println("Get PKCS#12 RSAPrivateCrtKey(" + jksPrivateCrtKey +
"): [Bit-Length: " + jksPrivateCrtKey.getModulus().bitLength() +
", Modulus: " + jksPrivateCrtKey.getModulus() +
", PublicExponent: " + jksPrivateCrtKey.getPublicExponent() +
", PrivateExponent: " + jksPrivateCrtKey.getPrivateExponent() +
", Prime-P: " + jksPrivateCrtKey.getPrimeP() +
", Prime-Q: " + jksPrivateCrtKey.getPrimeQ() +
", Prime-Exponent-P: " + jksPrivateCrtKey.getPrimeExponentP() +
", Prime-Exponent-Q: " + jksPrivateCrtKey.getPrimeExponentQ() +
", CRT-Coefficient: " + jksPrivateCrtKey.getCrtCoefficient()
);
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
System.exit(1);
}
//Get Certificate
Certificate jksCert = null;
try {
jksCert = jksKeyStore.getCertificate(jksAlias);
System.out.println("Get Certificate from PKCS#12: " + jksCert);
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
}
//Get Certificate Chain
Certificate[] jksCerts = null;
try {
jksCerts = jksKeyStore.getCertificateChain(jksAlias);
System.out.println("Get Certificate Chain from JKS, with " +
jksCerts.length + " certs.");
for (int i = 0; i < jksCerts.length; i++) {
System.out.println("Certificate " + (i + 1) +
" from JKS in the chain: " + jksCerts[i]);
}
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
}
//=====================================
// PKCS#12 stuff
//=====================================
KeyStore pkcs12KeyStore = null;
try {
pkcs12KeyStore = KeyStore.getInstance("PKCS12", "BC");
System.out.println("Create PKCS#12 KeyStore Object.");
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
}
try {
pkcs12KeyStore.load(null, pkcs12Password);
System.out.println(
"Load a new fresh PKCS#12 KeyStore from scratch.");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
} catch (CertificateException e) {
e.printStackTrace();
System.exit(1);
}
try {
pkcs12KeyStore.setKeyEntry(pkcs12Alias, jksPrivateCrtKey,
pkcs12Password, jksCerts);
System.out.println("Add the RSA Private Crt Key and the " +
"Certificate Chain to the PKCS#12 KeyStore.");
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
}
OutputStream pkcs12OutputStream = null;
try {
pkcs12OutputStream = new FileOutputStream(pkcs12FileName);
System.out.println(
"Establish PKCS#12 OutputStream to " + pkcs12FileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
try {
pkcs12KeyStore.store(pkcs12OutputStream, pkcs12Password);
pkcs12OutputStream.close();
System.out.println("Store PKCS#12 KeyStore: " + pkcs12FileName);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
} catch (CertificateException e) {
e.printStackTrace();
System.exit(1);
}
//=====================================
// Reread the pkcs12KeyStore
//=====================================
InputStream pkcs12InputStream = null;
try {
pkcs12InputStream = new FileInputStream(pkcs12FileName);
System.out.println(
"Establish PKCS#12 InputStream to " + pkcs12FileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
try {
pkcs12KeyStore.load(pkcs12InputStream, pkcs12Password);
System.out.println("Re-read the PKCS#12 KeyStore.");
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
System.exit(1);
} catch (CertificateException e) {
e.printStackTrace();
System.exit(1);
}
//Get PrivateKey
RSAPrivateCrtKey pkcs12PrivateCrtKey = null;
try {
pkcs12PrivateCrtKey =
(RSAPrivateCrtKey) pkcs12KeyStore.getKey(pkcs12Alias, pkcs12Password);
System.out.println(
"Get PKCS#12 RSAPrivateCrtKey(" + pkcs12PrivateCrtKey +
"): [Bit-Length: " + pkcs12PrivateCrtKey.getModulus().bitLength() +
", Modulus: " + pkcs12PrivateCrtKey.getModulus() +
", PublicExponent: " + pkcs12PrivateCrtKey.getPublicExponent() +
", PrivateExponent: " + pkcs12PrivateCrtKey.getPrivateExponent() +
", Prime-P: " + pkcs12PrivateCrtKey.getPrimeP() +
", Prime-Q: " + pkcs12PrivateCrtKey.getPrimeQ() +
", Prime-Exponent-P: " + pkcs12PrivateCrtKey.getPrimeExponentP() +
", Prime-Exponent-Q: " + pkcs12PrivateCrtKey.getPrimeExponentQ() +
", CRT-Coefficient: " + pkcs12PrivateCrtKey.getCrtCoefficient()
);
} catch (KeyStoreException e) {
e.printStackTrace();
System.exit(1);
} catch (NoSuchAlgorithmException e) {