{"record":{"id":"58e615cbeb23536b","repo":"TheAlgorithms/Java","slug":"message-must-be-smaller-than-the-prime-modulus-p","errorCode":null,"errorMessage":"Message must be smaller than the prime modulus p.","messagePattern":"Message must be smaller than the prime modulus p\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java","lineNumber":116,"sourceCode":"    /**\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    }\n\n    /**\n     * Decrypts a ciphertext using the private key.","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java#L98-L134","documentation":"Thrown by ElGamalCipher.encrypt when message.compareTo(p) >= 0, i.e. the message is not smaller than the prime modulus. ElGamal requires the message to lie in [0, p-1]; a message >= p cannot be uniquely recovered after modular reduction, so it is rejected.","triggerScenarios":"Passing a message whose BigInteger value is greater than or equal to the prime p. Happens when the encoded message is larger than the prime, or when p is small relative to the payload.","commonSituations":"Encrypting large payloads directly instead of using hybrid encryption (encrypt a symmetric key); a small test prime paired with a big message; message not reduced mod p.","solutions":["Keep messages strictly smaller than p; for larger data, use hybrid encryption (encrypt a symmetric key with ElGamal).","If appropriate, reduce the message mod p before encrypting (note this loses information if not reversible).","Use a sufficiently large prime for the expected message size."],"exampleFix":"// before\nCipherText ct = ElGamalCipher.encrypt(largeMessage, p, g, y);\n\n// after\n// Hybrid: ElGamal encrypts a random AES key, AES encrypts the payload\nSecretKey aesKey = generateAesKey();\nbyte[] enc = aesEncrypt(largeMessage, aesKey);\nBigInteger keyMsg = new BigInteger(1, aesKey.getEncoded());\nif (keyMsg.compareTo(p) >= 0) throw new IllegalStateException(\"key too large for prime\");\nCipherText ct = ElGamalCipher.encrypt(keyMsg, p, g, y);","handlingStrategy":"validation","validationCode":"if (message.compareTo(p) >= 0) {\n    throw new IllegalArgumentException(\"message must be < p; use hybrid encryption for large data\");\n}\nCipherText ct = ElGamalCipher.encrypt(message, p, g, y);","typeGuard":"static boolean inRange(BigInteger m, BigInteger p) {\n    return m != null && p != null && m.signum() >= 0 && m.compareTo(p) < 0;\n}","tryCatchPattern":"try {\n    CipherText ct = ElGamalCipher.encrypt(message, p, g, y);\n} catch (IllegalArgumentException e) {\n    // message >= p; switch to hybrid encryption\n}","preventionTips":["Use hybrid encryption (ElGamal on a symmetric key) for large payloads.","Choose a prime large enough for the message space.","Reduce or reject out-of-range messages upstream."],"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"}