TheAlgorithms/Java · error · IllegalArgumentException

Base, secret, and prime must be non-null and positive values

Error message

Base, secret, and prime must be non-null and positive values.

What it means

Thrown by the DiffieHellman constructor when any of base, secret, or prime is null OR has signum <= 0 (i.e. zero or negative). All three parameters must be strictly positive BigIntegers for the modPow computations to be valid.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/DiffieHellman.java:15

package com.thealgorithms.ciphers;

import java.math.BigInteger;

public final class DiffieHellman {

    private final BigInteger base;
    private final BigInteger secret;
    private final BigInteger prime;

    // Constructor to initialize base, secret, and prime
    public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) {
        // Check for non-null and positive values
        if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {
            throw new IllegalArgumentException("Base, secret, and prime must be non-null and positive values.");
        }
        this.base = base;
        this.secret = secret;
        this.prime = prime;
    }

    // Method to calculate public value (g^x mod p)
    public BigInteger calculatePublicValue() {
        // Returns g^x mod p
        return base.modPow(secret, prime);
    }

    // Method to calculate the shared secret key (otherPublic^secret mod p)
    public BigInteger calculateSharedSecret(BigInteger otherPublicValue) {
        if (otherPublicValue == null || otherPublicValue.signum() <= 0) {
            throw new IllegalArgumentException("Other public value must be non-null and positive.");
        }
        // Returns b^x mod p or a^y mod p

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure all three arguments are non-null and strictly positive (> 0) before constructing.
  2. Load prime from a trusted source and validate it is a probable prime.
  3. Reject zero/negative early in your parameter-loading code.

Example fix

// before
DiffieHellman dh = new DiffieHellman(base, secret, prime);

// after
if (base == null || secret == null || prime == null
        || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {
    throw new IllegalStateException("Invalid DH parameters");
}
DiffieHellman dh = new DiffieHellman(base, secret, prime);
Defensive patterns

Strategy: validation

Validate before calling

if (base == null || secret == null || prime == null
        || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {
    throw new IllegalStateException("Invalid DH parameters");
}
DiffieHellman dh = new DiffieHellman(base, secret, prime);

Type guard

static boolean validParams(BigInteger b, BigInteger s, BigInteger p) {
    return b != null && s != null && p != null
        && b.signum() > 0 && s.signum() > 0 && p.signum() > 0;
}

Try / catch

try {
    DiffieHellman dh = new DiffieHellman(base, secret, prime);
} catch (IllegalArgumentException e) {
    // a parameter was null or non-positive; reload valid values
}

Prevention

When it happens

Trigger: Passing null for any parameter, or a BigInteger equal to BigInteger.ZERO, or a negative BigInteger. A prime of 0 or 1 would also make modular arithmetic meaningless.

Common situations: Uninitialized config values (null); prime loaded as 0 due to a parse error; default placeholder values; secret derived from an empty/random source that produced 0.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/1e5e9acd3491495a. Report an issue: GitHub.