java.security
Class KeyFactory

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.security.KeyFactory

public class KeyFactory
extends Object sample code for java.lang.Object definition code for java.lang.Object

Key factories are used to convert keys (opaque cryptographic keys of type Key) into key specifications (transparent representations of the underlying key material), and vice versa.

Key factories are bi-directional. That is, they allow you to build an opaque key object from a given key specification (key material), or to retrieve the underlying key material of a key object in a suitable format.

Multiple compatible key specifications may exist for the same key. For example, a DSA public key may be specified using DSAPublicKeySpec or X509EncodedKeySpec. A key factory can be used to translate between compatible key specifications.

The following is an example of how to use a key factory in order to instantiate a DSA public key from its encoding. Assume Alice has received a digital signature from Bob. Bob also sent her his public key (in encoded format) to verify his signature. Alice then performs the following actions:

 X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(bobEncodedPubKey);
 KeyFactory keyFactory = KeyFactory.getInstance("DSA");
 PublicKey bobPubKey = keyFactory.generatePublic(bobPubKeySpec);
 Signature sig = Signature.getInstance("DSA");
 sig.initVerify(bobPubKey);
 sig.update(data);
 sig.verify(signature);
 

Since:
1.2
See Also:
Key sample code for java.security.Key definition code for java.security.Key , PublicKey sample code for java.security.PublicKey definition code for java.security.PublicKey , PrivateKey sample code for java.security.PrivateKey definition code for java.security.PrivateKey , KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec , DSAPublicKeySpec sample code for java.security.spec.DSAPublicKeySpec definition code for java.security.spec.DSAPublicKeySpec , X509EncodedKeySpec sample code for java.security.spec.X509EncodedKeySpec definition code for java.security.spec.X509EncodedKeySpec

Constructor Summary
protected KeyFactory sample code for java.security.KeyFactory.KeyFactory(java.security.KeyFactorySpi, java.security.Provider, java.lang.String) definition code for java.security.KeyFactory.KeyFactory(java.security.KeyFactorySpi, java.security.Provider, java.lang.String) (KeyFactorySpi sample code for java.security.KeyFactorySpi definition code for java.security.KeyFactorySpi  keyFacSpi, Provider sample code for java.security.Provider definition code for java.security.Provider  provider, String sample code for java.lang.String definition code for java.lang.String  algorithm)
          Creates a KeyFactory object.
 
Method Summary
 PrivateKey sample code for java.security.PrivateKey definition code for java.security.PrivateKey generatePrivate sample code for java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec) definition code for java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec) (KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec  keySpec)
          Generates a private key object from the provided key specification (key material).
 PublicKey sample code for java.security.PublicKey definition code for java.security.PublicKey generatePublic sample code for java.security.KeyFactory.generatePublic(java.security.spec.KeySpec) definition code for java.security.KeyFactory.generatePublic(java.security.spec.KeySpec) (KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec  keySpec)
          Generates a public key object from the provided key specification (key material).
 String sample code for java.lang.String definition code for java.lang.String getAlgorithm sample code for java.security.KeyFactory.getAlgorithm() definition code for java.security.KeyFactory.getAlgorithm() ()
          Gets the name of the algorithm associated with this KeyFactory.
static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String) definition code for java.security.KeyFactory.getInstance(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  algorithm)
          Generates a KeyFactory object that implements the specified algorithm.
static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String, java.security.Provider) definition code for java.security.KeyFactory.getInstance(java.lang.String, java.security.Provider) (String sample code for java.lang.String definition code for java.lang.String  algorithm, Provider sample code for java.security.Provider definition code for java.security.Provider  provider)
          Generates a KeyFactory object for the specified algorithm from the specified provider.
static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String, java.lang.String) definition code for java.security.KeyFactory.getInstance(java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  algorithm, String sample code for java.lang.String definition code for java.lang.String  provider)
          Generates a KeyFactory object for the specified algorithm from the specified provider.
<T extends KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec >
T
getKeySpec sample code for java.security.KeyFactory.getKeySpec(java.security.Key, java.lang.Class) definition code for java.security.KeyFactory.getKeySpec(java.security.Key, java.lang.Class) (Key sample code for java.security.Key definition code for java.security.Key  key, Class sample code for java.lang.Class definition code for java.lang.Class <T> keySpec)
          Returns a specification (key material) of the given key object.
 Provider sample code for java.security.Provider definition code for java.security.Provider getProvider sample code for java.security.KeyFactory.getProvider() definition code for java.security.KeyFactory.getProvider() ()
          Returns the provider of this key factory object.
 Key sample code for java.security.Key definition code for java.security.Key translateKey sample code for java.security.KeyFactory.translateKey(java.security.Key) definition code for java.security.KeyFactory.translateKey(java.security.Key) (Key sample code for java.security.Key definition code for java.security.Key  key)
          Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this key factory.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

KeyFactory sample code for java.security.KeyFactory(java.security.KeyFactorySpi, java.security.Provider, java.lang.String) definition code for java.security.KeyFactory(java.security.KeyFactorySpi, java.security.Provider, java.lang.String)

protected KeyFactory(KeyFactorySpi sample code for java.security.KeyFactorySpi definition code for java.security.KeyFactorySpi  keyFacSpi,
                     Provider sample code for java.security.Provider definition code for java.security.Provider  provider,
                     String sample code for java.lang.String definition code for java.lang.String  algorithm)
Creates a KeyFactory object.

Parameters:
keyFacSpi - the delegate
provider - the provider
algorithm - the name of the algorithm to associate with this KeyFactory
Method Detail

getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String) definition code for java.security.KeyFactory.getInstance(java.lang.String)

public static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory  getInstance(String sample code for java.lang.String definition code for java.lang.String  algorithm)
                              throws NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException 
Generates a KeyFactory object that implements the specified algorithm. If the default provider package provides an implementation of the requested algorithm, an instance of KeyFactory containing that implementation is returned. If the algorithm is not available in the default package, other packages are searched.

Parameters:
algorithm - the name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard algorithm names.
Returns:
a KeyFactory object for the specified algorithm.
Throws:
NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException - if the requested algorithm is not available in the default provider package or any of the other provider packages that were searched.

getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String, java.lang.String) definition code for java.security.KeyFactory.getInstance(java.lang.String, java.lang.String)

public static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory  getInstance(String sample code for java.lang.String definition code for java.lang.String  algorithm,
                                     String sample code for java.lang.String definition code for java.lang.String  provider)
                              throws NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException ,
                                     NoSuchProviderException sample code for java.security.NoSuchProviderException definition code for java.security.NoSuchProviderException 
Generates a KeyFactory object for the specified algorithm from the specified provider.

Parameters:
algorithm - the name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard algorithm names.
provider - the name of the provider.
Returns:
a KeyFactory object for the specified algorithm.
Throws:
NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException - if the algorithm is not available from the specified provider.
NoSuchProviderException sample code for java.security.NoSuchProviderException definition code for java.security.NoSuchProviderException - if the provider has not been configured.
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the provider name is null or empty.
See Also:
Provider sample code for java.security.Provider definition code for java.security.Provider

getInstance sample code for java.security.KeyFactory.getInstance(java.lang.String, java.security.Provider) definition code for java.security.KeyFactory.getInstance(java.lang.String, java.security.Provider)

public static KeyFactory sample code for java.security.KeyFactory definition code for java.security.KeyFactory  getInstance(String sample code for java.lang.String definition code for java.lang.String  algorithm,
                                     Provider sample code for java.security.Provider definition code for java.security.Provider  provider)
                              throws NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException 
Generates a KeyFactory object for the specified algorithm from the specified provider. Note: the provider doesn't have to be registered.

Parameters:
algorithm - the name of the requested key algorithm. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard algorithm names.
provider - the provider.
Returns:
a KeyFactory object for the specified algorithm.
Throws:
NoSuchAlgorithmException sample code for java.security.NoSuchAlgorithmException definition code for java.security.NoSuchAlgorithmException - if the algorithm is not available from the specified provider.
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the provider is null.
Since:
1.4
See Also:
Provider sample code for java.security.Provider definition code for java.security.Provider

getProvider sample code for java.security.KeyFactory.getProvider() definition code for java.security.KeyFactory.getProvider()

public final Provider sample code for java.security.Provider definition code for java.security.Provider  getProvider()
Returns the provider of this key factory object.

Returns:
the provider of this key factory object

getAlgorithm sample code for java.security.KeyFactory.getAlgorithm() definition code for java.security.KeyFactory.getAlgorithm()

public final String sample code for java.lang.String definition code for java.lang.String  getAlgorithm()
Gets the name of the algorithm associated with this KeyFactory.

Returns:
the name of the algorithm associated with this KeyFactory

generatePublic sample code for java.security.KeyFactory.generatePublic(java.security.spec.KeySpec) definition code for java.security.KeyFactory.generatePublic(java.security.spec.KeySpec)

public final PublicKey sample code for java.security.PublicKey definition code for java.security.PublicKey  generatePublic(KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec  keySpec)
                               throws InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException 
Generates a public key object from the provided key specification (key material).

Parameters:
keySpec - the specification (key material) of the public key.
Returns:
the public key.
Throws:
InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException - if the given key specification is inappropriate for this key factory to produce a public key.

generatePrivate sample code for java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec) definition code for java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec)

public final PrivateKey sample code for java.security.PrivateKey definition code for java.security.PrivateKey  generatePrivate(KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec  keySpec)
                                 throws InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException 
Generates a private key object from the provided key specification (key material).

Parameters:
keySpec - the specification (key material) of the private key.
Returns:
the private key.
Throws:
InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException - if the given key specification is inappropriate for this key factory to produce a private key.

getKeySpec sample code for java.security.KeyFactory.<T extends java.security.spec.KeySpec>getKeySpec(java.security.Key, java.lang.Class<T>) definition code for java.security.KeyFactory.<T extends java.security.spec.KeySpec>getKeySpec(java.security.Key, java.lang.Class<T>)

public final <T extends KeySpec sample code for java.security.spec.KeySpec definition code for java.security.spec.KeySpec > T getKeySpec(Key sample code for java.security.Key definition code for java.security.Key  key,
                                              Class sample code for java.lang.Class definition code for java.lang.Class <T> keySpec)
                                   throws InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException 
Returns a specification (key material) of the given key object. keySpec identifies the specification class in which the key material should be returned. It could, for example, be DSAPublicKeySpec.class, to indicate that the key material should be returned in an instance of the DSAPublicKeySpec class.

Parameters:
key - the key.
keySpec - the specification class in which the key material should be returned.
Returns:
the underlying key specification (key material) in an instance of the requested specification class.
Throws:
InvalidKeySpecException sample code for java.security.spec.InvalidKeySpecException definition code for java.security.spec.InvalidKeySpecException - if the requested key specification is inappropriate for the given key, or the given key cannot be processed (e.g., the given key has an unrecognized algorithm or format).

translateKey sample code for java.security.KeyFactory.translateKey(java.security.Key) definition code for java.security.KeyFactory.translateKey(java.security.Key)

public final Key sample code for java.security.Key definition code for java.security.Key  translateKey(Key sample code for java.security.Key definition code for java.security.Key  key)
                       throws InvalidKeyException sample code for java.security.InvalidKeyException definition code for java.security.InvalidKeyException 
Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this key factory.

Parameters:
key - the key whose provider is unknown or untrusted.
Returns:
the translated key.
Throws:
InvalidKeyException sample code for java.security.InvalidKeyException definition code for java.security.InvalidKeyException - if the given key cannot be processed by this key factory.