Key
) into key specifications
(transparent representations of the underlying key material), and vice
versa.
Key factories are bidirectional. 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);
Every implementation of the Java platform is required to support the
following standard KeyFactory
algorithms:
DiffieHellman
DSA
RSA
Key
, PublicKey
, PrivateKey
, java.security.spec.KeySpec
, java.security.spec.DSAPublicKeySpec
, java.security.spec.X509EncodedKeySpec
Modifier and Type | Field and Description |
---|---|
private final String | |
private static final Debug | |
private final Object | |
private Provider | |
private Iterator | |
private volatile KeyFactorySpi |
Access | Constructor and Description |
---|---|
protected | KeyFactory(KeyFactorySpi
the delegate keyFacSpi, Provider the provider provider, String the name of the algorithm
to associate with this algorithm)KeyFactory Creates a |
private |
Modifier and Type | Method and Description |
---|---|
public final PrivateKey | Returns: the private key.the specification (key material) of the private key. keySpec)Generates a private key object from the provided key specification (key material). |
public final PublicKey | Returns: the public key.the specification (key material) of the public key. keySpec)Generates a public key object from the provided key specification (key material). |
public final String | Returns: the name of the algorithm associated with thisKeyFactory Gets the name of the algorithm
associated with this |
public static KeyFactory | Returns: the newKeyFactory objectthe name of the requested key algorithm.
See the KeyFactory section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm)Returns a |
public static KeyFactory | Returns: the newKeyFactory objectthe name of the requested key algorithm.
See the KeyFactory section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm, String the name of the provider. provider)Returns a |
public static KeyFactory | Returns: the newKeyFactory objectthe name of the requested key algorithm.
See the KeyFactory section in the
Java Security Standard Algorithm Names Specification
for information about standard algorithm names. algorithm, Provider the provider. provider)Returns a |
public final < the type of the key specification to be returned T extends KeySpec> T | Returns: the underlying key specification (key material) in an instance of the requested specification class.the key. key, Class<T> the specification class in which
the key material should be returned. keySpec)Returns a specification (key material) of the given key object. |
public final Provider | Returns: the provider of this key factory objectReturns the provider of this key factory object. |
private KeyFactorySpi | nextSpi(KeyFactorySpi oldSpi)
Update the active |
public final Key | Returns: the translated key.the key whose provider is unknown or untrusted. key)Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this key factory. |
algorithm | back to summary |
---|---|
private final String algorithm |
debug | back to summary |
---|---|
private static final Debug debug |
lock | back to summary |
---|---|
private final Object lock |
provider | back to summary |
---|---|
private Provider provider |
serviceIterator | back to summary |
---|---|
private Iterator<Provider. |
spi | back to summary |
---|---|
private volatile KeyFactorySpi spi |
KeyFactory | back to summary |
---|---|
protected KeyFactory(KeyFactorySpi keyFacSpi, Provider provider, String algorithm) Creates a
|
KeyFactory | back to summary |
---|---|
private KeyFactory(String algorithm) throws NoSuchAlgorithmException |
generatePrivate | back to summary |
---|---|
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException Generates a private key object from the provided key specification (key material).
|
generatePublic | back to summary |
---|---|
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException Generates a public key object from the provided key specification (key material).
|
getAlgorithm | back to summary |
---|---|
public final String getAlgorithm() Gets the name of the algorithm
associated with this
|
getInstance | back to summary |
---|---|
public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException Returns a This method traverses the list of registered security providers,
starting with the most preferred provider.
A new Note that the list of registered providers may be retrieved via
the Implementation Note The JDK Reference Implementation additionally uses the
|
getInstance | back to summary |
---|---|
public static KeyFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException Returns a A new Note that the list of registered providers may be retrieved via
the
|
getInstance | back to summary |
---|---|
public static KeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException Returns a A new
|
getKeySpec | back to summary |
---|---|
public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException Returns a specification (key material) of the given key object.
|
getProvider | back to summary |
---|---|
public final Provider getProvider() Returns the provider of this key factory object.
|
nextSpi | back to summary |
---|---|
private KeyFactorySpi nextSpi(KeyFactorySpi oldSpi) Update the active |
translateKey | back to summary |
---|---|
public final Key translateKey(Key key) throws InvalidKeyException Translates a key object, whose provider may be unknown or potentially untrusted, into a corresponding key object of this key factory.
|