Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. I have a keystore.jks file with multiple certificate including public and private key
  2.  
  3. This "all-public-cert.crt" file contain only certificate (public key only) . should not contain any private key in this file.
  4.  
  5. String keystorePath = ....
  6. InputStream is = new FileInputStream(new File(keystorePath));
  7. KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
  8. keystore.load(is, "keystorePassword".toCharArray());
  9.  
  10. Enumeration e = ks.aliases();
  11. Set<Certificate> certs = new HashSet<Certificate>();
  12. while (e.hasMoreElements()) {
  13. Certificate cert = ks.getCertificate((String) enumeration.nextElement());
  14. certs.add(cert);
  15. }
  16.  
  17. String pathToStoreTheCerts = ...
  18. File path = new File(pathToStoreTheCerts);
  19. OutputStream os = null;
  20. for (X509Certificate cert : certs) {
  21. File certFile = new File(path, cert.getSubjectX500Principal().getName() + ".crt");
  22. os = new FileOutputStream(certFile);
  23. os.write(cert.getEncoded());
  24. os.flush();
  25. }
  26. os.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement