TheAlgorithms/Java · error · IllegalArgumentException

Bit length must be at least {} for security.

Error message

Bit length must be at least {} for security.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/ElGamalCipher.java:67

    /**
     * Container for the encryption result.
     *
     * @param a The first component (g^k mod p).
     * @param b The second component (y^k * m mod p).
     */
    public record CipherText(BigInteger a, BigInteger b) {
    }

    /**
     * Generates a valid ElGamal KeyPair using a Safe Prime.
     *
     * @param bitLength The bit length of the prime modulus p. Must be at least 256.
     * @return A valid KeyPair (p, g, y, x).
     * @throws IllegalArgumentException if bitLength is too small.
     */
    public static KeyPair generateKeys(int bitLength) {
        if (bitLength < MIN_BIT_LENGTH) {
            throw new IllegalArgumentException("Bit length must be at least " + MIN_BIT_LENGTH + " for security.");
        }

        BigInteger p;
        BigInteger q;
        BigInteger g;
        BigInteger x;
        BigInteger y;

        // Generate Safe Prime p = 2q + 1
        do {
            q = new BigInteger(bitLength - 1, PRIME_CERTAINTY, RANDOM);
            p = q.multiply(BigInteger.TWO).add(BigInteger.ONE);
        } while (!p.isProbablePrime(PRIME_CERTAINTY));

        // Find a Generator g (Primitive Root modulo p)
        do {
            g = new BigInteger(bitLength, RANDOM).mod(p.subtract(BigInteger.TWO)).add(BigInteger.TWO);
        } while (!isValidGenerator(g, p, q));

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Use a bit length of at least 256 (the documented minimum).
  2. For real security, prefer 2048 or higher; reserve 256 only for constrained/test scenarios.
  3. Validate bitLength >= 256 before calling and surface a clear configuration error.

Example fix

// before
KeyPair kp = ElGamalCipher.generateKeys(128);

// after
int bitLength = Math.max(requestedBits, 2048);
KeyPair kp = ElGamalCipher.generateKeys(bitLength);
Defensive patterns

Strategy: validation

Validate before calling

if (bitLength < 256) {
    throw new IllegalArgumentException("bitLength must be >= 256");
}
KeyPair kp = ElGamalCipher.generateKeys(bitLength);

Type guard

static boolean secureBitLength(int n) { return n >= 256; }

Try / catch

try {
    KeyPair kp = ElGamalCipher.generateKeys(bitLength);
} catch (IllegalArgumentException e) {
    // bitLength too small; raise it (prefer >= 2048)
    kp = ElGamalCipher.generateKeys(2048);
}

Prevention

When it happens

Trigger: Calling generateKeys(bitLength) with any value < 256 (e.g. 128 for a quick test, or 0/default).

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/63d791c656208da4. Report an issue: GitHub.