The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.security  [30 examples] > Key Store  [4 examples]

e211. 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) {
        }
    }

 Related Examples
e208. Listing the Aliases in a Key Store
e209. Retrieving a Certificate from a Key Store
e210. Retrieving a Key Pair from a Key Store

See also: Message Digests    Permissions    Policy Files    Public and Private Keys    Signatures   


© 2002 Addison-Wesley.