public class DHKeyPairGenerator
extends java.security.KeyPairGenerator
The Diffie Hellman algorithm has been the first public-key algorithm. It only can be used for key-agreement, but not for data encrypting and decrypting.
PKCS#3
describes a method for implementing the Diffie Hellman key agreement
where two (or more) entities use general Diffie Hellman parameters
(an odd prime p
, an integer base g
satisfying
0 < g < p
, and optionally an integer
l
prescribing the length of the private value), generated
from some central authority (which may an entity itself), to create a
shared secret, only known by them.
For creating a DH key pair necessary for performing a Diffie Hellman key
agreement, a KeyPairGenerator has to be instantiated, properly initialized
and directed to actually generate the keys by calling the
generateKeyPair
method. If the generator
is not initialized by explicitly calling an initialize
method,
the modulus length per default is set to 2048 bits.
Generating DH keys using a modulus length of, e.g. 1024 bits (explicitly initialized), may be done by:
KeyPairGenerator key_gen = KeyPairGenerator.getIntance("DH"); key_gen.initialize(1024, sec_random); KeyPair key_pair = key_gen.generateKeyPair();
The example above initializes the key pair generator algorithm-independently by only specifying the length of the modulus. For performing an algorithm-specific initialization, an explicit cast to DHKeyPairGenerator would be necessary, e.g.:
DHKeyPairGenerator dh_key_gen = (DHKeyPairGenerator)key_gen; dh_key_gen.initialize(dh_param_spec, sec_random);
KeyPairGenerator
,
KeyPair
,
DHPublicKey
,
DHPrivateKey
,
DHKeyFactory
,
DHKeyAgreement
,
DHParameterSpec
Constructor and Description |
---|
DHKeyPairGenerator()
Default constructor for creating a DHKeyPairGenerator object.
|
Modifier and Type | Method and Description |
---|---|
java.security.KeyPair |
generateKeyPair()
Actually generates the requested DH KeyPair.
|
void |
initialize(java.security.spec.AlgorithmParameterSpec param,
java.security.SecureRandom random)
Initializes this DHKeyPairGenerator with given DH parameter specification and random seed.
|
void |
initialize(int primeLength)
Initializes the DHKeyPairGenerator for given prime modulus length.
|
void |
initialize(int primeLength,
java.security.SecureRandom random)
Initializes the DHKeyPairGenerator for given prime modulus length with the given
random seed.
|
public DHKeyPairGenerator()
public void initialize(int primeLength)
initialize
in class java.security.KeyPairGenerator
primeLength
- the length of the prime modulus in bitspublic void initialize(int primeLength, java.security.SecureRandom random)
initialize
in class java.security.KeyPairGenerator
primeLength
- the length of the prime modulus in bitsrandom
- the random seed as SecureRandom.public void initialize(java.security.spec.AlgorithmParameterSpec param, java.security.SecureRandom random) throws java.security.InvalidAlgorithmParameterException
initialize
in class java.security.KeyPairGenerator
param
- the DHParameterSpec representing prime modulus p
, base
generator g
, and exponent length l
random
- the random seed as SecureRandomInvalidParameterException
- if the given algorithm parameter specification is not
a DHParameterSpec or the size of the exponent is not
shorter than that of the prime modulus, both derived
from the given DH parameter specificationjava.security.InvalidAlgorithmParameterException
public java.security.KeyPair generateKeyPair()
generateKeyPair
in class java.security.KeyPairGenerator