Package java.security
Class KeyFactory
java.lang.Object
java.security.KeyFactory
public class KeyFactory extends Object
KeyFactory is an engine class that can be used to translate between
public and private key objects and convert keys between their external
representation, that can be easily transported and their internal
representation.-
Constructor Summary
Constructors Modifier Constructor Description protectedKeyFactory(KeyFactorySpi keyFacSpi, Provider provider, String algorithm)Constructs a new instance ofKeyFactorywith the specified arguments. -
Method Summary
Modifier and Type Method Description PrivateKeygeneratePrivate(KeySpec keySpec)Generates a instance ofPrivateKeyfrom the given key specification.PublicKeygeneratePublic(KeySpec keySpec)Generates a instance ofPublicKeyfrom the given key specification.StringgetAlgorithm()Returns the name of the algorithm associated with thisKeyFactory.static KeyFactorygetInstance(String algorithm)Returns a new instance ofKeyFactorythat utilizes the specified algorithm.static KeyFactorygetInstance(String algorithm, String provider)Returns a new instance ofKeyFactorythat utilizes the specified algorithm from the specified provider.static KeyFactorygetInstance(String algorithm, Provider provider)Returns a new instance ofKeyFactorythat utilizes the specified algorithm from the specified provider.<T extends KeySpec>
TgetKeySpec(Key key, Class<T> keySpec)Returns the key specification for the specified key.ProvidergetProvider()Returns the provider associated with thisKeyFactory.KeytranslateKey(Key key)Translates the given key into a key from this key factory.
-
Constructor Details
-
KeyFactory
Constructs a new instance ofKeyFactorywith the specified arguments.- Parameters:
keyFacSpi- the concrete key factory service.provider- the provider.algorithm- the algorithm to use.
-
-
Method Details
-
getInstance
Returns a new instance ofKeyFactorythat utilizes the specified algorithm.- Parameters:
algorithm- the name of the algorithm.- Returns:
- a new instance of
KeyFactorythat utilizes the specified algorithm. - Throws:
NoSuchAlgorithmException- if no provider provides the requested algorithm.
-
getInstance
public static KeyFactory getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderExceptionReturns a new instance ofKeyFactorythat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm.provider- the name of the provider.- Returns:
- a new instance of
KeyFactorythat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the provider does not provide the requested algorithm.NoSuchProviderException- if the requested provider is not available.IllegalArgumentException- ifprovider == null || provider.isEmpty()
-
getInstance
public static KeyFactory getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmExceptionReturns a new instance ofKeyFactorythat utilizes the specified algorithm from the specified provider.- Parameters:
algorithm- the name of the algorithm.provider- the security provider.- Returns:
- a new instance of
KeyFactorythat utilizes the specified algorithm from the specified provider. - Throws:
NoSuchAlgorithmException- if the provider does not provide the requested algorithm.IllegalArgumentException- ifprovider == null
-
getProvider
Returns the provider associated with thisKeyFactory.- Returns:
- the provider associated with this
KeyFactory.
-
getAlgorithm
Returns the name of the algorithm associated with thisKeyFactory.- Returns:
- the name of the algorithm associated with this
KeyFactory.
-
generatePublic
Generates a instance ofPublicKeyfrom the given key specification.- Parameters:
keySpec- the specification of the public key- Returns:
- the public key
- Throws:
InvalidKeySpecException- if the specifiedkeySpecis invalid
-
generatePrivate
Generates a instance ofPrivateKeyfrom the given key specification.- Parameters:
keySpec- the specification of the private key.- Returns:
- the private key.
- Throws:
InvalidKeySpecException- if the specifiedkeySpecis invalid.
-
getKeySpec
public final <T extends KeySpec> T getKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecExceptionReturns the key specification for the specified key.- Parameters:
key- the key from which the specification is requested.keySpec- the type of the requestedKeySpec.- Returns:
- the key specification for the specified key.
- Throws:
InvalidKeySpecException- if the key can not be processed, or the requested requestedKeySpecis inappropriate for the given key.
-
translateKey
Translates the given key into a key from this key factory.- Parameters:
key- the key to translate.- Returns:
- the translated key.
- Throws:
InvalidKeyException- if the specified key can not be translated by this key factory.
-