FileDocCategorySizeDatePackage
KeyFactory.javaAPI DocJava SE 5 API13431Fri Aug 26 14:57:14 BST 2005java.security

KeyFactory

public class KeyFactory extends 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);
author
Jan Luehe
version
1.32, 05/05/04
see
Key
see
PublicKey
see
PrivateKey
see
java.security.spec.KeySpec
see
java.security.spec.DSAPublicKeySpec
see
java.security.spec.X509EncodedKeySpec
since
1.2

Fields Summary
private static final Debug
debug
private final String
algorithm
private Provider
provider
private volatile KeyFactorySpi
spi
private final Object
lock
private Iterator
serviceIterator
Constructors Summary
protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, String algorithm)
Creates a KeyFactory object.

param
keyFacSpi the delegate
param
provider the provider
param
algorithm the name of the algorithm to associate with this KeyFactory

    
                                 
        
			   
	this.spi = keyFacSpi;
	this.provider = provider;
	this.algorithm = algorithm;
    
private KeyFactory(String algorithm)

	this.algorithm = algorithm;
	List<Service> list = GetInstance.getServices("KeyFactory", algorithm);
	serviceIterator = list.iterator();
	// fetch and instantiate initial spi
	if (nextSpi(null) == null) {
	    throw new NoSuchAlgorithmException
	    	(algorithm + " KeyFactory not available");
	}
    
Methods Summary
public final java.security.PrivateKeygeneratePrivate(java.security.spec.KeySpec keySpec)
Generates a private key object from the provided key specification (key material).

param
keySpec the specification (key material) of the private key.
return
the private key.
exception
InvalidKeySpecException if the given key specification is inappropriate for this key factory to produce a private key.

	if (serviceIterator == null) {
	    return spi.engineGeneratePrivate(keySpec);
	}
	Exception failure = null;
	KeyFactorySpi mySpi = spi;
	do {
	    try {
		return mySpi.engineGeneratePrivate(keySpec);
	    } catch (Exception e) {
		if (failure == null) {
		    failure = e;
		}
		mySpi = nextSpi(mySpi);
	    }
	} while (mySpi != null);
	if (failure instanceof RuntimeException) {
	    throw (RuntimeException)failure;
	}
	if (failure instanceof InvalidKeySpecException) {
	    throw (InvalidKeySpecException)failure;
	}
	throw new InvalidKeySpecException
		("Could not generate private key", failure);
    
public final java.security.PublicKeygeneratePublic(java.security.spec.KeySpec keySpec)
Generates a public key object from the provided key specification (key material).

param
keySpec the specification (key material) of the public key.
return
the public key.
exception
InvalidKeySpecException if the given key specification is inappropriate for this key factory to produce a public key.

	if (serviceIterator == null) {
	    return spi.engineGeneratePublic(keySpec);
	}
	Exception failure = null;
	KeyFactorySpi mySpi = spi;
	do {
	    try {
		return mySpi.engineGeneratePublic(keySpec);
	    } catch (Exception e) {
		if (failure == null) {
		    failure = e;
		}
		mySpi = nextSpi(mySpi);
	    }
	} while (mySpi != null);
	if (failure instanceof RuntimeException) {
	    throw (RuntimeException)failure;
	}
	if (failure instanceof InvalidKeySpecException) {
	    throw (InvalidKeySpecException)failure;
	}
	throw new InvalidKeySpecException
		("Could not generate public key", failure);
    
public final java.lang.StringgetAlgorithm()
Gets the name of the algorithm associated with this KeyFactory.

return
the name of the algorithm associated with this KeyFactory

	return this.algorithm;
    
public static java.security.KeyFactorygetInstance(java.lang.String algorithm)
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.

param
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.
return
a KeyFactory object for the specified algorithm.
exception
NoSuchAlgorithmException if the requested algorithm is not available in the default provider package or any of the other provider packages that were searched.

 
	return new KeyFactory(algorithm);
    
public static java.security.KeyFactorygetInstance(java.lang.String algorithm, java.lang.String provider)
Generates a KeyFactory object for the specified algorithm from the specified provider.

param
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.
param
provider the name of the provider.
return
a KeyFactory object for the specified algorithm.
exception
NoSuchAlgorithmException if the algorithm is not available from the specified provider.
exception
NoSuchProviderException if the provider has not been configured.
exception
IllegalArgumentException if the provider name is null or empty.
see
Provider

	Instance instance = GetInstance.getInstance("KeyFactory", 
	    KeyFactorySpi.class, algorithm, provider);
	return new KeyFactory((KeyFactorySpi)instance.impl,
	    instance.provider, algorithm);
    
public static java.security.KeyFactorygetInstance(java.lang.String algorithm, java.security.Provider provider)
Generates a KeyFactory object for the specified algorithm from the specified provider. Note: the provider doesn't have to be registered.

param
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.
param
provider the provider.
return
a KeyFactory object for the specified algorithm.
exception
NoSuchAlgorithmException if the algorithm is not available from the specified provider.
exception
IllegalArgumentException if the provider is null.
see
Provider
since
1.4

	Instance instance = GetInstance.getInstance("KeyFactory", 
	    KeyFactorySpi.class, algorithm, provider);
	return new KeyFactory((KeyFactorySpi)instance.impl,
	    instance.provider, algorithm);
    
public final TgetKeySpec(java.security.Key key, java.lang.Class keySpec)
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.

param
key the key.
param
keySpec the specification class in which the key material should be returned.
return
the underlying key specification (key material) in an instance of the requested specification class.
exception
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).

	if (serviceIterator == null) {
	    return spi.engineGetKeySpec(key, keySpec);
	}
	Exception failure = null;
	KeyFactorySpi mySpi = spi;
	do {
	    try {
		return mySpi.engineGetKeySpec(key, keySpec);
	    } catch (Exception e) {
		if (failure == null) {
		    failure = e;
		}
		mySpi = nextSpi(mySpi);
	    }
	} while (mySpi != null);
	if (failure instanceof RuntimeException) {
	    throw (RuntimeException)failure;
	}
	if (failure instanceof InvalidKeySpecException) {
	    throw (InvalidKeySpecException)failure;
	}
	throw new InvalidKeySpecException
		("Could not get key spec", failure);
    
public final java.security.ProvidergetProvider()
Returns the provider of this key factory object.

return
the provider of this key factory object

	synchronized (lock) {
	    // disable further failover after this call
	    serviceIterator = null;
	    return provider;
	}
    
private java.security.KeyFactorySpinextSpi(java.security.KeyFactorySpi oldSpi)
Update the active KeyFactorySpi of this class and return the next implementation for failover. If no more implemenations are available, this method returns null. However, the active spi of this class is never set to null.

	synchronized (lock) {
	    // somebody else did a failover concurrently
	    // try that spi now
	    if ((oldSpi != null) && (oldSpi != spi)) {
		return spi;
	    }
	    if (serviceIterator == null) {
		return null;
	    }
	    while (serviceIterator.hasNext()) {
		Service s = serviceIterator.next();
		try {
		    Object obj = s.newInstance(null);
		    if (obj instanceof KeyFactorySpi == false) {
			continue;
		    }
		    KeyFactorySpi spi = (KeyFactorySpi)obj;
		    provider = s.getProvider();
		    this.spi = spi;
		    return spi;
		} catch (NoSuchAlgorithmException e) {
		    // ignore
		}
	    }
	    serviceIterator = null;
	    return null;
	}
    
public final java.security.KeytranslateKey(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.

param
key the key whose provider is unknown or untrusted.
return
the translated key.
exception
InvalidKeyException if the given key cannot be processed by this key factory.

	if (serviceIterator == null) {
	    return spi.engineTranslateKey(key);
	}
	Exception failure = null;
	KeyFactorySpi mySpi = spi;
	do {
	    try {
		return mySpi.engineTranslateKey(key);
	    } catch (Exception e) {
		if (failure == null) {
		    failure = e;
		}
		mySpi = nextSpi(mySpi);
	    }
	} while (mySpi != null);
	if (failure instanceof RuntimeException) {
	    throw (RuntimeException)failure;
	}
	if (failure instanceof InvalidKeyException) {
	    throw (InvalidKeyException)failure;
	}
	throw new InvalidKeyException
		("Could not translate key", failure);