{"record":{"id":"1e5e9acd3491495a","repo":"TheAlgorithms/Java","slug":"base-secret-and-prime-must-be-non-null-and-posit","errorCode":null,"errorMessage":"Base, secret, and prime must be non-null and positive values.","messagePattern":"Base, secret, and prime must be non-null and positive values\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/DiffieHellman.java","lineNumber":15,"sourceCode":"package com.thealgorithms.ciphers;\n\nimport java.math.BigInteger;\n\npublic final class DiffieHellman {\n\n    private final BigInteger base;\n    private final BigInteger secret;\n    private final BigInteger prime;\n\n    // Constructor to initialize base, secret, and prime\n    public DiffieHellman(BigInteger base, BigInteger secret, BigInteger prime) {\n        // 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","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/DiffieHellman.java#L1-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure all three arguments are non-null and strictly positive (> 0) before constructing.","Load prime from a trusted source and validate it is a probable prime.","Reject zero/negative early in your parameter-loading code."],"exampleFix":"// before\nDiffieHellman dh = new DiffieHellman(base, secret, prime);\n\n// after\nif (base == null || secret == null || prime == null\n        || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {\n    throw new IllegalStateException(\"Invalid DH parameters\");\n}\nDiffieHellman dh = new DiffieHellman(base, secret, prime);","handlingStrategy":"validation","validationCode":"if (base == null || secret == null || prime == null\n        || base.signum() <= 0 || secret.signum() <= 0 || prime.signum() <= 0) {\n    throw new IllegalStateException(\"Invalid DH parameters\");\n}\nDiffieHellman dh = new DiffieHellman(base, secret, prime);","typeGuard":"static boolean validParams(BigInteger b, BigInteger s, BigInteger p) {\n    return b != null && s != null && p != null\n        && b.signum() > 0 && s.signum() > 0 && p.signum() > 0;\n}","tryCatchPattern":"try {\n    DiffieHellman dh = new DiffieHellman(base, secret, prime);\n} catch (IllegalArgumentException e) {\n    // a parameter was null or non-positive; reload valid values\n}","preventionTips":["Load the prime from a trusted source and verify it is a probable prime.","Reject zero/negative parameters at the loading layer.","Validate all three before construction to give a clearer error."],"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"}