{"record":{"id":"9582ab5443c71b7c","repo":"TheAlgorithms/Java","slug":"message-must-be-non-negative","errorCode":null,"errorMessage":"Message must be non-negative.","messagePattern":"Message must be non-negative\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java","lineNumber":113,"sourceCode":"        return new KeyPair(p, g, y, x);\n    }\n\n    /**\n     * Encrypts a message using the public key.\n     *\n     * @param message The message converted to BigInteger.\n     * @param p       The prime modulus.\n     * @param g       The generator.\n     * @param y       The public key component.\n     * @return The CipherText pair (a, b).\n     * @throws IllegalArgumentException if inputs are null, negative, or message >= p.\n     */\n    public static CipherText encrypt(BigInteger message, BigInteger p, BigInteger g, BigInteger y) {\n        if (message == null || p == null || g == null || y == null) {\n            throw new IllegalArgumentException(\"Inputs cannot be null.\");\n        }\n        if (message.compareTo(BigInteger.ZERO) < 0) {\n            throw new IllegalArgumentException(\"Message must be non-negative.\");\n        }\n        if (message.compareTo(p) >= 0) {\n            throw new IllegalArgumentException(\"Message must be smaller than the prime modulus p.\");\n        }\n\n        BigInteger k;\n        BigInteger pMinus1 = p.subtract(BigInteger.ONE);\n\n        // Select ephemeral key k such that 1 < k < p-1 and gcd(k, p-1) = 1\n        do {\n            k = new BigInteger(p.bitLength(), RANDOM);\n        } while (k.compareTo(BigInteger.ONE) <= 0 || k.compareTo(pMinus1) >= 0 || !k.gcd(pMinus1).equals(BigInteger.ONE));\n\n        BigInteger a = g.modPow(k, p);\n        BigInteger b = y.modPow(k, p).multiply(message).mod(p);\n\n        return new CipherText(a, b);\n    }","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java#L95-L131","documentation":"Thrown by ElGamalCipher.encrypt when the message BigInteger is negative (compareTo(ZERO) < 0). ElGamal encrypts a message in the range [0, p-1]; a negative message has no valid representation under the modulus, so it is rejected after the null check and before the upper-bound check.","triggerScenarios":"Passing a message whose BigInteger value is negative. Common when converting a signed numeric or a hash interpreted as signed.","commonSituations":"Message derived from arithmetic that can go negative; interpreting a byte array's leading bit as a sign; converting a long with the high bit set via BigInteger.valueOf.","solutions":["Ensure the message BigInteger is non-negative before encrypting.","Construct BigIntegers from byte arrays using the 1-arg BigInteger(byte[]) constructor only after confirming the sign, or use BigInteger(1, bytes) to force positive.","Reduce or reject negative values upstream."],"exampleFix":"// before\nBigInteger msg = new BigInteger(messageBytes);\nCipherText ct = ElGamalCipher.encrypt(msg, p, g, y);\n\n// after\nBigInteger msg = new BigInteger(1, messageBytes); // force non-negative\nif (msg.compareTo(p) >= 0) msg = msg.mod(p);\nCipherText ct = ElGamalCipher.encrypt(msg, p, g, y);","handlingStrategy":"validation","validationCode":"if (message == null || message.signum() < 0) {\n    throw new IllegalArgumentException(\"message must be non-negative\");\n}\nCipherText ct = ElGamalCipher.encrypt(message, p, g, y);","typeGuard":"static boolean isNonNegative(BigInteger m) { return m != null && m.signum() >= 0; }","tryCatchPattern":"try {\n    CipherText ct = ElGamalCipher.encrypt(message, p, g, y);\n} catch (IllegalArgumentException e) {\n    // message negative; rebuild as positive using BigInteger(1, bytes)\n}","preventionTips":["Build messages with new BigInteger(1, bytes) to force non-negative.","Avoid BigInteger.valueOf on signed longs with the high bit set.","Validate sign before encrypting."],"tags":["cryptography","ciphers","elgamal","java","validation","biginteger","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}