public class KeyAgreement extends Object
The most common form of key agreement used today is the Diffie-Hellman key exchange algorithm, described in PKCS #3 - Diffie Hellman Key Agreement Standard.
KeyGenerator
,
SecretKey
Modifier | Constructor and Description |
---|---|
protected |
KeyAgreement(KeyAgreementSpi kaSpi,
Provider provider,
String algorithm) |
Modifier and Type | Method and Description |
---|---|
Key |
doPhase(Key key,
boolean lastPhase)
Do a phase in the key agreement.
|
byte[] |
generateSecret()
Generate the shared secret in a new byte array.
|
int |
generateSecret(byte[] sharedSecret,
int offset)
Generate the shared secret and store it into the supplied array.
|
SecretKey |
generateSecret(String algorithm)
Generate the shared secret and return it as an appropriate
SecretKey . |
String |
getAlgorithm()
Return the name of this key-agreement algorithm.
|
static KeyAgreement |
getInstance(String algorithm)
Get an implementation of an algorithm from the first provider that
implements it.
|
static KeyAgreement |
getInstance(String algorithm,
Provider provider)
Return an implementation of an algorithm from a specific provider.
|
static KeyAgreement |
getInstance(String algorithm,
String provider)
Return an implementation of an algorithm from a named provider.
|
Provider |
getProvider()
Return the provider of the underlying implementation.
|
void |
init(Key key)
Initialize this key agreement with a key.
|
void |
init(Key key,
AlgorithmParameterSpec params)
Initialize this key agreement with a key and parameters.
|
void |
init(Key key,
AlgorithmParameterSpec params,
SecureRandom random)
Initialize this key agreement with a key, parameters, and source of
randomness.
|
void |
init(Key key,
SecureRandom random)
Initialize this key agreement with a key and a source of
randomness.
|
protected KeyAgreement(KeyAgreementSpi kaSpi, Provider provider, String algorithm)
public static final KeyAgreement getInstance(String algorithm) throws NoSuchAlgorithmException
algorithm
- The name of the algorithm to get.NoSuchAlgorithmException
- If the specified algorithm is not
implemented by any installed provider.IllegalArgumentException
- if algorithm
is
null
or is an empty string.public static final KeyAgreement getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
algorithm
- The name of the algorithm to create.provider
- The name of the provider from which to get the
implementation.NoSuchAlgorithmException
- If the named provider does not implement
the algorithm.NoSuchProviderException
- If the named provider does not exist.IllegalArgumentException
- if either algorithm
or
provider
is null
, or if
algorithm
is an empty string.public static final KeyAgreement getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
algorithm
- The name of the algorithm to get.provider
- The provider from which to get the implementation.NoSuchAlgorithmException
- If this provider does not implement the
algorithm.IllegalArgumentException
- if either algorithm
or
provider
is null
, or if
algorithm
is an empty string.public final Key doPhase(Key key, boolean lastPhase) throws IllegalStateException, InvalidKeyException
lastPhase
flag set to true
.key
- The key for this phase.lastPhase
- Should be true
if this will be the
last phase before generating the shared secret.null
if there is
no intermediate result.IllegalStateException
- If this instance has not
been initialized.InvalidKeyException
- If the key is
inappropriate for this algorithm.public final byte[] generateSecret() throws IllegalStateException
IllegalStateException
- If this instnace has not
been initialized, or if not enough calls to
doPhase
have been made.public final int generateSecret(byte[] sharedSecret, int offset) throws IllegalStateException, ShortBufferException
sharedSecret
- The array in which to store the secret.offset
- The index in sharedSecret
to start
storing data.IllegalStateException
- If this instnace has not
been initialized, or if not enough calls to
doPhase
have been made.ShortBufferException
- If the supplied array is
not large enough to store the result.public final SecretKey generateSecret(String algorithm) throws IllegalStateException, InvalidKeyException, NoSuchAlgorithmException
SecretKey
.algorithm
- The secret key's algorithm.IllegalStateException
- If this instnace has not
been initialized, or if not enough calls to
doPhase
have been made.InvalidKeyException
- If the shared secret
cannot be used to make a SecretKey
.NoSuchAlgorithmException
- If the specified
algorithm does not exist.public final String getAlgorithm()
public final Provider getProvider()
public final void init(Key key) throws InvalidKeyException
SecureRandom
as its source
of randomness.key
- The key, usually the user's private key.InvalidKeyException
- If the supplied key is
not appropriate.public final void init(Key key, SecureRandom random) throws InvalidKeyException
key
- The key, usually the user's private key.random
- The source of randomness.InvalidKeyException
- If the supplied key is
not appropriate.public final void init(Key key, AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException, InvalidKeyException
SecureRandom
as its source of randomness.key
- The key, usually the user's private key.params
- The algorithm parameters.InvalidAlgorithmParameterException
- If the
supplied parameters are not appropriate.InvalidKeyException
- If the supplied key is
not appropriate.public final void init(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidAlgorithmParameterException, InvalidKeyException
key
- The key, usually the user's private key.params
- The algorithm parameters.random
- The source of randomness.InvalidAlgorithmParameterException
- If the
supplied parameters are not appropriate.InvalidKeyException
- If the supplied key is
not appropriate.