Yu-Jack
10/5/2018 - 10:27 AM

Create p12

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] privateKeyBytes = .....;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey rsaPrivateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
store.load(null, null);
Certificate[] certificates = { certificate };
store.setKeyEntry("key", rsaPrivateKey, null, certificates);

ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
store.store(bOutput, password);
return bOutput.toByteArray();