FileDocCategorySizeDatePackage
KeyAgreement.javaAPI DocAndroid 1.5 API12611Wed May 06 22:41:02 BST 2009javax.crypto

KeyAgreement

public class KeyAgreement extends Object
This class provides the functionality for a key exchange protocol. This enables two or more parties to agree on a secret key for symmetric cryptography.
since
Android 1.0

Fields Summary
private static final org.apache.harmony.security.fortress.Engine
engine
private static final SecureRandom
rndm
private final Provider
provider
private final KeyAgreementSpi
spiImpl
private final String
algorithm
Constructors Summary
protected KeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider, String algorithm)
Creates a new {@code KeyAgreement} instance.

param
keyAgreeSpi the SPI delegate.
param
provider the provider providing this KeyAgreement.
param
algorithm the name of the key agreement algorithm.
since
Android 1.0


                                                                         
        
              
        this.provider = provider;
        this.algorithm = algorithm;
        this.spiImpl = keyAgreeSpi;
    
Methods Summary
public final java.security.KeydoPhase(java.security.Key key, boolean lastPhase)
Does the next (or the last) phase of the key agreement, using the specified key.

param
key the key received from the other party for this phase.
param
lastPhase set to {@code true} if this is the last phase of this key agreement.
return
the intermediate key from this phase or {@code null} if there is no intermediate key for this phase.
throws
InvalidKeyException if the specified key cannot be used in this key agreement or this phase,
throws
IllegalStateException if this instance has not been initialized.
since
Android 1.0

        return spiImpl.engineDoPhase(key, lastPhase);
    
public final byte[]generateSecret()
Generates the shared secret.

return
the generated shared secret.
throws
IllegalStateException if this key agreement is not complete.
since
Android 1.0

        return spiImpl.engineGenerateSecret();
    
public final intgenerateSecret(byte[] sharedSecret, int offset)
Generates the shared secret and stores it into the buffer {@code sharedSecred} at {@code offset}.

param
sharedSecret the buffer to store the shared secret.
param
offset the offset in the buffer.
return
the number of bytes stored in the buffer.
throws
IllegalStateException if this key agreement is not complete.
throws
ShortBufferException if the specified buffer is too small for the shared secret.
since
Android 1.0

        return spiImpl.engineGenerateSecret(sharedSecret, offset);
    
public final javax.crypto.SecretKeygenerateSecret(java.lang.String algorithm)
Generates the shared secret.

param
algorithm the algorithm to for the {@code SecretKey}
return
the shared secret as a {@code SecretKey} of the specified algorithm.
throws
IllegalStateException if this key agreement is not complete.
throws
NoSuchAlgorithmException if the specified algorithm for the secret key does not exists.
throws
InvalidKeyException if a {@code SecretKey} with the specified algorithm cannot be created using the generated shared secret.
since
Android 1.0

        return spiImpl.engineGenerateSecret(algorithm);
    
public final java.lang.StringgetAlgorithm()
Returns the name of the key agreement algorithm.

return
the name of the key agreement algorithm.
since
Android 1.0

        return algorithm;
    
public static final javax.crypto.KeyAgreementgetInstance(java.lang.String algorithm)
Creates a new {@code KeyAgreement} for the specified algorithm.

param
algorithm the name of the key agreement algorithm to create.
return
a key agreement for the specified algorithm.
throws
NoSuchAlgorithmException if no installed provider can provide the requested algorithm.
throws
NullPointerException if the specified algorithm is {@code null}.
since
Android 1.0

        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, null);
            return new KeyAgreement((KeyAgreementSpi) engine.spi, engine.provider,
                    algorithm);
        }
    
public static final javax.crypto.KeyAgreementgetInstance(java.lang.String algorithm, java.lang.String provider)
Creates a new {@code KeyAgreement} for the specified algorithm from the specified provider.

param
algorithm the name of the key agreement algorithm to create.
param
provider the name of the provider that provides the requested algorithm.
return
a key agreement for the specified algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified provider cannot provide the requested algorithm.
throws
NoSuchProviderException if the specified provider does not exist.
throws
IllegalArgumentException if the specified provider name is {@code null} or empty.
since
Android 1.0

        if ((provider == null) || (provider.length() == 0)) {
            throw new IllegalArgumentException(Messages.getString("crypto.03")); //$NON-NLS-1$
        }
        Provider impProvider = Security.getProvider(provider);
        if (impProvider == null) {
            throw new NoSuchProviderException(provider);
        }
        return getInstance(algorithm, impProvider);
    
public static final javax.crypto.KeyAgreementgetInstance(java.lang.String algorithm, java.security.Provider provider)
Create a new {@code KeyAgreement} for the specified algorithm from the specified provider.

param
algorithm the name of the key agreement algorithm to create.
param
provider the provider that provides the requested algorithm.
return
a key agreement for the specified algorithm from the specified provider.
throws
NoSuchAlgorithmException if the specified provider cannot provide the requested algorithm.
throws
IllegalArgumentException if the specified provider is {@code null}.
throws
NullPointerException if the specified algorithm name is {@code null}.

        if (provider == null) {
            throw new IllegalArgumentException(Messages.getString("crypto.04")); //$NON-NLS-1$
        }
        if (algorithm == null) {
            throw new NullPointerException(Messages.getString("crypto.02")); //$NON-NLS-1$
        }
        synchronized (engine) {
            engine.getInstance(algorithm, provider, null);
            return new KeyAgreement((KeyAgreementSpi) engine.spi, provider,
                    algorithm);
        }
    
public final java.security.ProvidergetProvider()
Returns the provider for this {@code KeyAgreement} instance.

return
the provider for this {@code KeyAgreement} instance.
since
Android 1.0

        return provider;
    
public final voidinit(java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)
Initializes this {@code KeyAgreement} with the specified key, algorithm parameters and randomness source.

param
key the key to initialize this key agreement.
param
params the parameters for this key agreement algorithm.
param
random the source for any randomness needed.
throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.
throws
InvalidAlgorithmParameterException if the specified parameters are invalid for this key agreement algorithm.
since
Android 1.0

        spiImpl.engineInit(key, params, random);
    
public final voidinit(java.security.Key key)
Initializes this {@code KeyAgreement} with the specified key.

param
key the key to initialize this key agreement.
throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.
since
Android 1.0

        spiImpl.engineInit(key, rndm);//new SecureRandom());
    
public final voidinit(java.security.Key key, java.security.SecureRandom random)
Initializes this {@code KeyAgreement} with the specified key and the specified randomness source.

param
key the key to initialize this key agreement.
param
random the source for any randomness needed.
throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.
since
Android 1.0

        spiImpl.engineInit(key, random);
    
public final voidinit(java.security.Key key, java.security.spec.AlgorithmParameterSpec params)
Initializes this {@code KeyAgreement} with the specified key and the algorithm parameters.

param
key the key to initialize this key agreement.
param
params the parameters for this key agreement algorithm.
throws
InvalidKeyException if the specified key cannot be used to initialize this key agreement.
throws
InvalidAlgorithmParameterException if the specified parameters are invalid for this key agreement algorithm.
since
Android 1.0

        spiImpl.engineInit(key, params, rndm);//new SecureRandom());