TheAlgorithms/Java · error · IllegalArgumentException

Inputs cannot be null.

Error message

Inputs cannot be null.

What it means

Thrown by ElGamalCipher.encrypt(BigInteger, BigInteger, BigInteger, BigInteger) when any of message, p, g, or y is null. This is the first of three sequential guards in encrypt; it rejects null inputs before the value-range checks.

Source

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

        // Compute Public Key y = g^x mod p
        y = g.modPow(x, p);

        return new KeyPair(p, g, y, x);
    }

    /**
     * Encrypts a message using the public key.
     *
     * @param message The message converted to BigInteger.
     * @param p       The prime modulus.
     * @param g       The generator.
     * @param y       The public key component.
     * @return The CipherText pair (a, b).
     * @throws IllegalArgumentException if inputs are null, negative, or message >= p.
     */
    public static CipherText encrypt(BigInteger message, BigInteger p, BigInteger g, BigInteger y) {
        if (message == null || p == null || g == null || y == null) {
            throw new IllegalArgumentException("Inputs cannot be null.");
        }
        if (message.compareTo(BigInteger.ZERO) < 0) {
            throw new IllegalArgumentException("Message must be non-negative.");
        }
        if (message.compareTo(p) >= 0) {
            throw new IllegalArgumentException("Message must be smaller than the prime modulus p.");
        }

        BigInteger k;
        BigInteger pMinus1 = p.subtract(BigInteger.ONE);

        // Select ephemeral key k such that 1 < k < p-1 and gcd(k, p-1) = 1
        do {
            k = new BigInteger(p.bitLength(), RANDOM);
        } while (k.compareTo(BigInteger.ONE) <= 0 || k.compareTo(pMinus1) >= 0 || !k.gcd(pMinus1).equals(BigInteger.ONE));

        BigInteger a = g.modPow(k, p);
        BigInteger b = y.modPow(k, p).multiply(message).mod(p);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Ensure all four arguments are non-null before encrypting.
  2. Generate keys via generateKeys first and pass its components directly.
  3. Add null checks in your data-loading layer.

Example fix

// before
CipherText ct = ElGamalCipher.encrypt(msg, p, g, y);

// after
Objects.requireNonNull(message, "message");
Objects.requireNonNull(p, "p");
Objects.requireNonNull(g, "g");
Objects.requireNonNull(y, "y");
CipherText ct = ElGamalCipher.encrypt(message, p, g, y);
Defensive patterns

Strategy: validation

Validate before calling

Objects.requireNonNull(message, "message");
Objects.requireNonNull(p, "p");
Objects.requireNonNull(g, "g");
Objects.requireNonNull(y, "y");
CipherText ct = ElGamalCipher.encrypt(message, p, g, y);

Type guard

static boolean allNonNull(Object... objs) {
    for (Object o : objs) if (o == null) return false;
    return true;
}

Try / catch

try {
    CipherText ct = ElGamalCipher.encrypt(message, p, g, y);
} catch (IllegalArgumentException e) {
    // an input was null; ensure keys are generated/loaded
}

Prevention

When it happens

Trigger: Calling encrypt with any argument null. Subsequent guards then check message sign (error 32) and message < p (error 33).

Common situations: A key component (p, g, y) not yet generated; message not initialized; deserialization producing null fields.

Related errors


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