Adding a Certificate to a Key Store

// This method adds a certificate with the specified alias to the specified keystore file. public static void addToKeyStore(File keystoreFile, char[] keystorePassword, String alias, java.security.cert.Certificate cert) { try { // Create an empty keystore object KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); // Load the keystore contents FileInputStream in = new FileInputStream(keystoreFile); keystore.load(in, keystorePassword); in.close(); // Add the certificate keystore.setCertificateEntry(alias, cert); // Save the new keystore contents FileOutputStream out = new FileOutputStream(keystoreFile); keystore.store(out, keystorePassword); out.close(); } catch (java.security.cert.CertificateException e) { } catch (NoSuchAlgorithmException e) { } catch (FileNotFoundException e) { // Keystore does not exist } catch (KeyStoreException e) { } catch (IOException e) { } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.