Package java.security

Class 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. A key factory can be used to translate between compatible key specifications.

    Since:
    1.2
    See Also:
    Key, PublicKey, PrivateKey, KeySpec
    • Method Detail

      • getInstance

        public static KeyFactory getInstance​(String algorithm)
                                      throws NoSuchAlgorithmException
        Returns a KeyFactory object that converts public/private keys of the specified algorithm.
        Parameters:
        algorithm - the name of the requested key algorithm. See the KeyFactory section in the Java Security Standard Algorithm Names Specification for information about standard algorithm names. Consult the release documentation for your implementation to see which algorithms are supported.
        Returns:
        the new KeyFactory object.
        Throws:
        NoSuchAlgorithmException - if the KeyFactorySpi has no implementation for the specified algorithm.
      • getAlgorithm

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

        public final PrivateKey generatePrivate​(KeySpec keySpec)
                                         throws 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 - if the given key specification is inappropriate for this key factory to produce a private key.
      • generatePublic

        public final PublicKey generatePublic​(KeySpec keySpec)
                                       throws 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 - if the given key specification is inappropriate for this key factory to produce a public key.