The Java Developers Almanac 1.4


Order this book from Amazon.

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

e210. Retrieving a Key Pair from a Key Store

This example retrieves from a keystore, the private and public key associated with an alias. To load a keystore, see e208 Listing the Aliases in a Key Store.
    public KeyPair getPrivateKey(KeyStore keystore, String alias, char[] password) {
        try {
            // Get private key
            Key key = keystore.getKey(alias, password);
            if (key instanceof PrivateKey) {
                // Get certificate of public key
                java.security.cert.Certificate cert = keystore.getCertificate(alias);
    
                // Get public key
                PublicKey publicKey = cert.getPublicKey();
    
                // Return a key pair
                return new KeyPair(publicKey, (PrivateKey)key);
            }
        } catch (UnrecoverableKeyException e) {
        } catch (NoSuchAlgorithmException e) {
        } catch (KeyStoreException e) {
        }
        return null;
    }

 Related Examples
e208. Listing the Aliases in a Key Store
e209. Retrieving a Certificate from a Key Store
e211. Adding a Certificate to a Key Store

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


© 2002 Addison-Wesley.