{"record":{"id":"f0dab0d7845c458b","repo":"TheAlgorithms/Java","slug":"other-public-value-must-be-non-null-and-positive","errorCode":null,"errorMessage":"Other public value must be non-null and positive.","messagePattern":"Other public value must be non-null and positive\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/DiffieHellman.java","lineNumber":31,"sourceCode":"        // Check for non-null and positive values\n        if (base == null || secret == null || prime == null || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {\n            throw new IllegalArgumentException(\"Base, secret, and prime must be non-null and positive values.\");\n        }\n        this.base = base;\n        this.secret = secret;\n        this.prime = prime;\n    }\n\n    // Method to calculate public value (g^x mod p)\n    public BigInteger calculatePublicValue() {\n        // Returns g^x mod p\n        return base.modPow(secret, prime);\n    }\n\n    // Method to calculate the shared secret key (otherPublic^secret mod p)\n    public BigInteger calculateSharedSecret(BigInteger otherPublicValue) {\n        if (otherPublicValue == null || otherPublicValue.signum() <= 0) {\n            throw new IllegalArgumentException(\"Other public value must be non-null and positive.\");\n        }\n        // Returns b^x mod p or a^y mod p\n        return otherPublicValue.modPow(secret, prime);\n    }\n}\n","sourceCodeStart":13,"sourceCodeEnd":37,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java#L13-L37","documentation":"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.","triggerScenarios":"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.","commonSituations":"Network deserialization yielding null; an attacker or buggy peer sending 0; public value not yet computed when the call is made.","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."],"exampleFix":"// before\nBigInteger shared = dh.calculateSharedSecret(peerPublic);\n\n// after\nif (peerPublic == null || peerPublic.signum() <= 0) {\n    throw new IllegalStateException(\"Invalid peer public value\");\n}\nBigInteger shared = dh.calculateSharedSecret(peerPublic);","handlingStrategy":"validation","validationCode":"if (otherPublicValue == null || otherPublicValue.signum() <= 0) {\n    throw new IllegalStateException(\"Invalid peer public value\");\n}\nBigInteger shared = dh.calculateSharedSecret(otherPublicValue);","typeGuard":"static boolean validPeerValue(BigInteger v) { return v != null && v.signum() > 0; }","tryCatchPattern":"try {\n    BigInteger shared = dh.calculateSharedSecret(peerPublic);\n} catch (IllegalArgumentException e) {\n    // peer value was null/non-positive; request retransmission\n}","preventionTips":["Validate received public values for non-null and positivity.","For stronger safety, check 1 < value < prime.","Reject zero values from untrusted/buggy peers explicitly."],"tags":["cryptography","ciphers","diffie-hellman","java","validation","biginteger","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}