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
- Validate the received public value is non-null and positive before computing the shared secret.
- Sanity-check the peer value is within expected range (1 < value < prime) for stronger guarantees.
- 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
- Validate received public values for non-null and positivity.
- For stronger safety, check 1 < value < prime.
- Reject zero values from untrusted/buggy peers explicitly.
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
- Base, secret, and prime must be non-null and positive values
- Message must be non-negative.
- Message must be smaller than the prime modulus p.
- Invalid Baconian code: {}
- DES key must be supplied as a 64 character binary string
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/f0dab0d7845c458b.
Report an issue: GitHub.