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

        BigInteger p = dhParams.getP();

        BigInteger result = pub.getY().modPow(privateValue, p);
        if (result.compareTo(ONE) == 0)
        {
            throw new IllegalStateException("Shared key can't be 1");
        }

        return message.modPow(key.getX(), p).multiply(result).mod(p);
    }
Weaknesses in this category are related to errors in the management of cryptographic keys.