TheAlgorithms/Java · error · IllegalArgumentException

Other public value must be non-null and positive.

Error message

Other public value must be non-null and positive.

What it means

Thrown by DiffieHellman.calculateSharedSecret(BigInteger) when the peer's public value is null or non-positive (<= 0). The shared secret is computed as otherPublicValue.modPow(secret, prime), which requires a valid positive peer value.

Source

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

        // 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
        return otherPublicValue.modPow(secret, prime);
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate the received public value is non-null and positive before computing the shared secret.
  2. Sanity-check the peer value is within expected range (1 < value < prime) for stronger guarantees.
  3. Handle missing/invalid peer data with a clear error rather than passing it through.

Example fix

// before
BigInteger shared = dh.calculateSharedSecret(peerPublic);

// after
if (peerPublic == null || peerPublic.signum() <= 0) {
    throw new IllegalStateException("Invalid peer public value");
}
BigInteger shared = dh.calculateSharedSecret(peerPublic);
Defensive patterns

Strategy: validation

Validate before calling

if (otherPublicValue == null || otherPublicValue.signum() <= 0) {
    throw new IllegalStateException("Invalid peer public value");
}
BigInteger shared = dh.calculateSharedSecret(otherPublicValue);

Type guard

static boolean validPeerValue(BigInteger v) { return v != null && v.signum() > 0; }

Try / catch

try {
    BigInteger shared = dh.calculateSharedSecret(peerPublic);
} catch (IllegalArgumentException e) {
    // peer value was null/non-positive; request retransmission
}

Prevention

When it happens

Trigger: Passing null, zero, or a negative BigInteger as the other party's public value. Also triggered by receiving a malformed/zero public value over the network.

Common situations: Network deserialization yielding null; an attacker or buggy peer sending 0; public value not yet computed when the call is made.

Related errors


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