{"record":{"id":"63d791c656208da4","repo":"TheAlgorithms/Java","slug":"bit-length-must-be-at-least-for-security","errorCode":null,"errorMessage":"Bit length must be at least {} for security.","messagePattern":"Bit length must be at least (.+?) for security\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java","lineNumber":67,"sourceCode":"    /**\n     * Container for the encryption result.\n     *\n     * @param a The first component (g^k mod p).\n     * @param b The second component (y^k * m mod p).\n     */\n    public record CipherText(BigInteger a, BigInteger b) {\n    }\n\n    /**\n     * Generates a valid ElGamal KeyPair using a Safe Prime.\n     *\n     * @param bitLength The bit length of the prime modulus p. Must be at least 256.\n     * @return A valid KeyPair (p, g, y, x).\n     * @throws IllegalArgumentException if bitLength is too small.\n     */\n    public static KeyPair generateKeys(int bitLength) {\n        if (bitLength < MIN_BIT_LENGTH) {\n            throw new IllegalArgumentException(\"Bit length must be at least \" + MIN_BIT_LENGTH + \" for security.\");\n        }\n\n        BigInteger p;\n        BigInteger q;\n        BigInteger g;\n        BigInteger x;\n        BigInteger y;\n\n        // Generate Safe Prime p = 2q + 1\n        do {\n            q = new BigInteger(bitLength - 1, PRIME_CERTAINTY, RANDOM);\n            p = q.multiply(BigInteger.TWO).add(BigInteger.ONE);\n        } while (!p.isProbablePrime(PRIME_CERTAINTY));\n\n        // Find a Generator g (Primitive Root modulo p)\n        do {\n            g = new BigInteger(bitLength, RANDOM).mod(p.subtract(BigInteger.TWO)).add(BigInteger.TWO);\n        } while (!isValidGenerator(g, p, q));","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java#L49-L85","documentation":"Thrown by ElGamalCipher.generateKeys(int) when bitLength is less than MIN_BIT_LENGTH (256). Small prime moduli are cryptographically insecure, so the library refuses to generate keys below this floor. The placeholder is filled with the constant MIN_BIT_LENGTH value.","triggerScenarios":"Calling generateKeys(bitLength) with any value < 256 (e.g. 128 for a quick test, or 0/default).","commonSituations":"Using a small bit length for fast local testing and forgetting to raise it for production; passing a default int of 0; copying example code with a toy value.","solutions":["Use a bit length of at least 256 (the documented minimum).","For real security, prefer 2048 or higher; reserve 256 only for constrained/test scenarios.","Validate bitLength >= 256 before calling and surface a clear configuration error."],"exampleFix":"// before\nKeyPair kp = ElGamalCipher.generateKeys(128);\n\n// after\nint bitLength = Math.max(requestedBits, 2048);\nKeyPair kp = ElGamalCipher.generateKeys(bitLength);","handlingStrategy":"validation","validationCode":"if (bitLength < 256) {\n    throw new IllegalArgumentException(\"bitLength must be >= 256\");\n}\nKeyPair kp = ElGamalCipher.generateKeys(bitLength);","typeGuard":"static boolean secureBitLength(int n) { return n >= 256; }","tryCatchPattern":"try {\n    KeyPair kp = ElGamalCipher.generateKeys(bitLength);\n} catch (IllegalArgumentException e) {\n    // bitLength too small; raise it (prefer >= 2048)\n    kp = ElGamalCipher.generateKeys(2048);\n}","preventionTips":["Default production bit lengths to 2048+.","Do not ship test-sized primes (128/256) to production.","Validate configuration before key generation."],"tags":["cryptography","ciphers","elgamal","java","security","validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}