Secret key Factory Keyfactory in Java

Source: Internet
Author: User

I. Overview

This class is located under the Java.security package, declared: Publicclasskeyfactory extends Object

The key factory is used to convert a key (an opaque encryption Key key of type) into a key specification (a transparent representation of the underlying key material) and vice versa.

The key factory is bidirectional. That is, they allow for the construction of opaque key objects based on a given key specification (keying material), and also to obtain the underlying keying material for the key object in the appropriate format.

There can be more than one compatible key specification for the same key. For example, you can use DSAPublicKeySpec or X509EncodedKeySpec specify a DSA public key. A key factory can be used to convert between compatible key specifications.

The following is an example of how to instantiate a DSA public key based on its encoding using a key factory. Suppose Alice received a digital signature from Bob. Bob also sends her public key (in encoded format) to verify his signature. Then Alice 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);

Second, the construction method

keyfactory (Keyfactoryspi keyfacspi,provider Provider, String algorithm)  Creates a Keyfactory object.
  
 
Parameters: keyFacSpi -Agent provider -Provided by algorithm -With this keyfactory The associated algorithm name
Iii. details of the method
1. Public Static Keyfactory getinstance (String algorithm) throws NoSuchAlgorithmException returns the Keyfactory object that transforms the Public/private keyword of the specified algorithm

This method iterates through the list of registered security providers starting with the preferred Provider. Returns a new Keyfactory object that encapsulates the implementation of the KEYFACTORYSPI, taken from the first Provider that supports a specified algorithm.

Note that the list of registered providers can be obtained by means of the Security.getProviders() method.

Parameters: algorithm -the name of the request key algorithm.
returns: the new Keyfactory object.
thrown: NoSuchAlgorithmException -If no Provider supports the KEYFACTORYSPI implementation of the specified algorithm.
2, public static keyfactory getinstance(String algorithm,string provider) throws NoSuchAlgorithmException, Nosuchproviderexception returns a Keyfactory object that transforms the Public/private keyword of the specified algorithm.

Returns a new Keyfactory object that encapsulates the implementation of the KEYFACTORYSPI, taken from the specified provider. Specifies that the provider must be registered in the Security provider list.

Note that the list of registered providers can be obtained by means of the Security.getProviders() method.

Parameters:
algorithm -The name of the request key algorithm. For information on standard algorithm names, see Appendix A in Java Cryptography Architecture API Specification & Reference.
provider -The name of the provider.
Return:
the new Keyfactory object.
Thrown:
NoSuchAlgorithmException -If the KEYFACTORYSPI implementation of the specified algorithm cannot be obtained from the specified provider.
NoSuchProviderException -If the specified provider is not registered in the Security provider list.
IllegalArgumentException -If the name of the provider is null or empty.
3. Public Static Keyfactory getinstance (String algorithm, Provider Provider) c9> throws NoSuchAlgorithmException
returns the Keyfactory object that transforms the Public/private keyword of the specified algorithm.

Returns a new Keyfactory object that encapsulates the implementation of the KEYFACTORYSPI, which is taken from the specified Provider object. Note that the specified Provider object does not need to be registered in the provider list.

Parameters:
algorithm -The name of the request key algorithm. For information on standard algorithm names, see Appendix A in Java Cryptography Architecture API Specification & Reference.
provider -provider.
Return:
The new Keyfactory object.
Thrown:
NoSuchAlgorithmException -If the KEYFACTORYSPI implementation of the specified algorithm cannot be obtained from the specified Provider object.
IllegalArgumentException -If the specified provider is null.
4. Public final Provider getprovider () returns the provider of this key factory object.
5. Public Final String getalgorithm () Gets the name of the algorithm associated with this keyfactory .
6. Public Final PublicKey generatepublic (KeySpec KeySpec) throws Invalidkeyspecexception
Generates a public key object based on the provided key specification (keying material).
Parameters:
keySpec -The specification of the public key (keying material).
Return:
public key.
Thrown:
InvalidKeySpecException -If the given key specification does not fit this key factory to generate the public key.
7. Public final privatekey generateprivate (KeySpec KeySpec) th Rows invalidkeyspecexception generates a private key object based on the supplied key specification (keying material).
Parameters:
keySpec -The specification of the private key (keying material).
Return:
The
private key.
Thrown:
InvalidKeySpecException -If the given key specification does not fit this key factory to generate the private key.
8. Public final <t extends KeySpec > T getkeyspec (key key, Class <T> keySpec) throws Invalidkeyspecexception returns the specification (keying material) for a given key object. keySpecidentifies the canonical class that should accept the return keying material. For example, it may be DSAPublicKeySpec.class that the keying material should be returned to DSAPublicKeySpec an instance of the class.
Parameters:
key -Key.
keySpec -The canonical class that returns the keying material should be accepted.
Return:
the underlying key specification (keying material) in the request specification class instance.
Thrown:
InvalidKeySpecException -If the requested key specification does not fit the given key, or the given key cannot be processed (for example, a given key has an unrecognized algorithm or format).
9. Public final Key translatekey (key key) throws Invalidkey Exception converts a provider may be an unknown or untrusted key object to the corresponding key object for this key factory.
Parameters:
key-the provider is an unknown or untrusted key object.
Return:
The converted key.
Thrown:
InvalidKeyException-If this key factory cannot process the given key.

Secret key Factory Keyfactory in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.