![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e210. Retrieving a Key Pair from a Key StoreThis 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; }
e209. Retrieving a Certificate from a Key Store e211. Adding a Certificate to a Key Store
© 2002 Addison-Wesley. |