FileDocCategorySizeDatePackage
DHAgreement.javaAPI DocAndroid 1.5 API2864Wed May 06 22:41:06 BST 2009org.bouncycastle.crypto.agreement

DHAgreement

public class DHAgreement extends Object
a Diffie-Hellman key exchange engine.

note: This uses MTI/A0 key agreement in order to make the key agreement secure against passive attacks. If you're doing Diffie-Hellman and both parties have long term public keys you should look at using this. For further information have a look at RFC 2631.

It's possible to extend this to more than two parties as well, for the moment that is left as an exercise for the reader.

Fields Summary
private org.bouncycastle.crypto.params.DHPrivateKeyParameters
key
private org.bouncycastle.crypto.params.DHParameters
dhParams
private BigInteger
privateValue
private SecureRandom
random
Constructors Summary
Methods Summary
public java.math.BigIntegercalculateAgreement(org.bouncycastle.crypto.params.DHPublicKeyParameters pub, java.math.BigInteger message)
given a message from a given party and the coresponding public key calculate the next message in the agreement sequence. In this case this will represent the shared secret.

        if (!pub.getParameters().equals(dhParams))
        {
            throw new IllegalArgumentException("Diffie-Hellman public key has wrong parameters.");
        }

        return message.modPow(key.getX(), dhParams.getP()).multiply(pub.getY().modPow(privateValue, dhParams.getP())).mod(dhParams.getP());
    
public java.math.BigIntegercalculateMessage()
calculate our initial message.

        this.privateValue = new BigInteger(
                                    dhParams.getP().bitLength() - 1, 0, random);

        return dhParams.getG().modPow(privateValue, dhParams.getP());
    
public voidinit(org.bouncycastle.crypto.CipherParameters param)

        AsymmetricKeyParameter  kParam;

        if (param instanceof ParametersWithRandom)
        {
            ParametersWithRandom    rParam = (ParametersWithRandom)param;

            this.random = rParam.getRandom();
            kParam = (AsymmetricKeyParameter)rParam.getParameters();
        }
        else
        {
            this.random = new SecureRandom();
            kParam = (AsymmetricKeyParameter)param;
        }

        
        if (!(kParam instanceof DHPrivateKeyParameters))
        {
            throw new IllegalArgumentException("DHEngine expects DHPrivateKeyParameters");
        }

        this.key = (DHPrivateKeyParameters)kParam;
        this.dhParams = key.getParameters();