Package java.security

Class 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 Detail

      • KeyFactory

        protected KeyFactory​(KeyFactorySpi keyFacSpi,
                             Provider provider,
                             String algorithm)
        Constructs a new instance of KeyFactory with the specified arguments.
        Parameters:
        keyFacSpi - the concrete key factory service.
        provider - the provider.
        algorithm - the algorithm to use.
    • Method Detail

      • getInstance

        public static KeyFactory getInstance​(String algorithm)
                                      throws NoSuchAlgorithmException
        Returns a new instance of KeyFactory that utilizes the specified algorithm.
        Parameters:
        algorithm - the name of the algorithm.
        Returns:
        a new instance of KeyFactory that utilizes the specified algorithm.
        Throws:
        NoSuchAlgorithmException - if no provider provides the requested algorithm.
      • getInstance

        public static KeyFactory getInstance​(String algorithm,
                                             Provider provider)
                                      throws NoSuchAlgorithmException
        Returns a new instance of KeyFactory that utilizes the specified algorithm from the specified provider. The provider supplied does not have to be registered.
        Parameters:
        algorithm - the name of the algorithm.
        provider - the security provider.
        Returns:
        a new instance of KeyFactory that utilizes the specified algorithm from the specified provider.
        Throws:
        NoSuchAlgorithmException - if the provider does not provide the requested algorithm.
        IllegalArgumentException - if provider == null
      • getProvider

        public final Provider getProvider()
        Returns the provider associated with this KeyFactory.
        Returns:
        the provider associated with this KeyFactory.
      • getAlgorithm

        public final String getAlgorithm()
        Returns the name of the algorithm associated with this KeyFactory.
        Returns:
        the name of the algorithm associated with this KeyFactory.
      • generatePublic

        public final PublicKey generatePublic​(KeySpec keySpec)
                                       throws InvalidKeySpecException
        Generates a instance of PublicKey from the given key specification.
        Parameters:
        keySpec - the specification of the public key
        Returns:
        the public key
        Throws:
        InvalidKeySpecException - if the specified keySpec is invalid
      • generatePrivate

        public final PrivateKey generatePrivate​(KeySpec keySpec)
                                         throws InvalidKeySpecException
        Generates a instance of PrivateKey from the given key specification.
        Parameters:
        keySpec - the specification of the private key.
        Returns:
        the private key.
        Throws:
        InvalidKeySpecException - if the specified keySpec is invalid.
      • getKeySpec

        public final <T extends KeySpec> T getKeySpec​(Key key,
                                                      Class<T> keySpec)
                                               throws InvalidKeySpecException
        Returns the key specification for the specified key.
        Parameters:
        key - the key from which the specification is requested.
        keySpec - the type of the requested KeySpec.
        Returns:
        the key specification for the specified key.
        Throws:
        InvalidKeySpecException - if the key can not be processed, or the requested requested KeySpec is inappropriate for the given key.
      • translateKey

        public final Key translateKey​(Key key)
                               throws InvalidKeyException
        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.