RSA keys Asymmetric Keys

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