DSA keys Asymmetric Keys

Generating a key pair is a very time−consuming operation. Fortunately, it does not need to be performed often; much of the time, we obtain keys from a key management system rather than generating them. However, when we establish our own key management system in the next chapter, well need to use this class; it is often easier to generate your own keys from scratch rather than use a key management system as well.

9.2.1.1 Using the KeyPairGenerator class

Like all engine classes, the KeyPairGenerator is an abstract class for which there is no implementation in the core API. However, it is possible to retrieve instances of the KeyPairGenerator class via these methods: public static KeyPairGenerator getInstanceString algorithm public static KeyPairGenerator getInstanceString algorithm, String provider Find the implementation of the engine that generates key pairs with the named algorithm. The algorithm should be one of the standard API algorithm names; if an appropriate implementation cannot be found, this method throws a NoSuchAlgorithmException . The first format of this method searches all available providers according to the rules we outlined in Chapter 8. The second method searches only the named provider, throwing a NoSuchProviderException if that provider has not been loaded. These methods search the providers that have been registered with the security provider interface for a key pair generator that supports the named algorithm. The names supported by the standard Sun security provider are DSA and RSA; the JCE security provider also supports the name Diffie−Hellman. The JSSE security provider supplies its own implementation of RSA keys. Once we have the key pair generator, we can invoke any of the following methods on it: public String getAlgorithm Return the name of the algorithm that this key pair generator implements e.g., DSA. public void initializeint strength public abstract void initializeint strength, SecureRandom random Initialize the key pair generator to generate keys of the given strength. The idea of strength is common among key pair generator algorithms; typically it means the number of bits that are used as input to the engine to calculate the key pair, but the actual meaning may vary between algorithms. Most key algorithms restrict the values that are valid for strength . In the case of DSA and Diffie−Hellman, the strength must be between 512 and 1024 and it must be a multiple of 64. For RSA, the strength must be any number between 512 and 2048. If an invalid number is passed for strength , an InvalidParameterException will be thrown. Key pairs require a random number generator to assist them. You may specify a particular random number generator if desired; otherwise, a default random number generator an instance of the SecureRandom class is used. public void initializeAlgorithmParameterSpec params public void initializeAlgorithmParameterSpec params, SecureRandom random Initialize the key pair generator using the given parameter specification which well discuss a little later. By default, the first method simply calls the second method with a default instance of the 144 SecureRandom class. public abstract KeyPair generateKeyPair public final KeyPair genKeyPair Generate a key pair using the initialization parameters previously specified. A KeyPairGenerator object can repeatedly generate key pairs by calling one of these methods; each new call generates a new key pair. The genKeyPair method simply calls the generateKeyPair method. Using these methods, generating a pair of keys is very straightforward: KeyPairGenerator kpg = KeyPairGenerator.getInstanceDSA; kpg.initialize512; KeyPair kp = kpg.generateKeyPair ; According to the Java documentation, you are allowed to generate a key pair without initializing the generator; in this situation, a default strength and random number generator are to be used. However, this feature has not always worked: a NullPointerException is sometimes thrown from within the generateKeyPair method. Since it is possible that third−party providers may behave similarly, it is always best to initialize the key pair generator. Well show what to do with these keys in the next chapter when we discuss the topic of key management.

9.2.1.2 Generating DSA keys

The abstraction provided by the key pair generator is usually all we need to generate keys. However, sometimes the particular algorithm needs additional information to generate a key pair. When a DSA key pair is generated, default values for P, Q, and G are used; in the Sun security provider, these values are precomputed to support strength values of 512 and 1024. Precomputing these values greatly reduces the time required to calculate a DSA key. Third−party DSA providers may provide precomputed values for additional strength values. It is possible to ask the key generator to use different values for P, Q, and G if the key pair generator supports the DSAKeyPairGenerator interface java.security.interfaces.DSAKeyPairGenerator : public interface DSAKeyPairGenerator Provide a mechanism by which the DSA−specific parameters of the key pair engine can be manipulated. There are two methods in this interface: public void initializeint modlen, boolean genParams, SecureRandom random Initialize the DSA key pair generator. The modulus length is the number of bits used to calculate the parameters; this must be any multiple of 64 between 512 and 1024. If genParams is true , then the P, Q, and G parameters will be generated for this new modulus length; otherwise, a precomputed value will be used but precomputed values in the Sun security provider are available only for modlen values of 512 and 1024. If the modulus length is invalid, this method throws an InvalidParameterException . public void initializeDSAParams params, SecureRandom random