This class translates asymmetric cryptographic keys between the two representations used by the Java Security API. java.security.Key is the opaque, algorithm-independent representation of a key used by most of the Security API. java.security.spec.KeySpec is a marker interface implemented by transparent, algorithm-specific representations of keys. KeyFactory is used with public and private keys; see javax.crypto.SecretKeyFactory if you are working with symmetric or secret keys.
To convert a Key to a KeySpec or vice versa, create a KeyFactory by calling one of the static getInstance( ) factory methods specifying the name of the key algorithm (e.g., DSA or RSA) and optionally specifying the name or Provider object for the desired provider. Then, use generatePublic( ) or generatePrivate( ) to create a PublicKey or PrivateKey object from a corresponding KeySpec. Or use getKeySpec( ) to obtain a KeySpec for a given Key. Because there can be more than one KeySpec implementation used by a particular cryptographic algorithm, you must also specify the Class of the KeySpec you desire.
If you do not need to transport keys portably between applications and/or systems, you can use a KeyStore to store and retrieve keys and certificates, avoiding KeySpec and KeyFactory altogether.
public classKeyFactory { // Protected Constructors protected
KeyFactory (KeyFactorySpi
keyFacSpi , Provider
provider , String
algorithm ); // Public Class Methods public static KeyFactory
getInstance (String
algorithm ) throws NoSuchAlgorithmException; public static KeyFactory
getInstance (String
algorithm , String
provider ) throws NoSuchAlgorithmException, NoSuchProviderException;
1.4 public static KeyFactory
getInstance (String
algorithm , Provider
provider ) throws NoSuchAlgorithmException; // Public Instance Methods public final PrivateKey
generatePrivate (java.security.spec.KeySpec
keySpec ) throws java.security.spec.InvalidKeySpecException; public final PublicKey
generatePublic (java.security.spec.KeySpec
keySpec ) throws java.security.spec.InvalidKeySpecException; public final String
getAlgorithm ( ); public final <T extends java.security.spec.KeySpec> T
getKeySpec (Key
key , Class<T>
keySpec ) throws java.security.spec.InvalidKeySpecException; public final Provider
getProvider ( ); public final Key
translateKey (Key
key ) throws InvalidKeyException; }