TheAlgorithms/Java · error · IllegalArgumentException

Message is empty

Error message

Message is empty

What it means

RSA.encrypt(String) guards against an empty plaintext because converting an empty string to bytes yields a zero-length array, and new BigInteger(emptyBytes) is undefined / produces BigInteger zero, which encrypts to a meaningless value. The library treats an empty message as a caller bug rather than silently producing junk ciphertext.

Source

Thrown at src/main/java/com/thealgorithms/ciphers/RSA.java:53

    /**
     * Constructor that generates RSA keys with the specified number of bits.
     *
     * @param bits The bit length of the keys to be generated. Common sizes include 512, 1024, 2048, etc.
     */
    public RSA(int bits) {
        generateKeys(bits);
    }

    /**
     * Encrypts a text message using the RSA public key.
     *
     * @param message The plaintext message to be encrypted.
     * @throws IllegalArgumentException If the message is empty.
     * @return The encrypted message represented as a String.
     */
    public synchronized String encrypt(String message) {
        if (message.isEmpty()) {
            throw new IllegalArgumentException("Message is empty");
        }
        return (new BigInteger(message.getBytes())).modPow(publicKey, modulus).toString();
    }

    /**
     * Encrypts a BigInteger message using the RSA public key.
     *
     * @param message The plaintext message as a BigInteger.
     * @return The encrypted message as a BigInteger.
     */
    public synchronized BigInteger encrypt(BigInteger message) {
        return message.modPow(publicKey, modulus);
    }

    /**
     * Decrypts an encrypted message (as String) using the RSA private key.
     *
     * @param encryptedMessage The encrypted message to be decrypted, represented as a String.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that the message is non-empty before calling encrypt.
  2. Use the BigInteger overload rsa.encrypt(new BigInteger(message.getBytes())) only when you have a real value.
  3. Treat an empty message as a no-op in your flow and skip encryption.

Example fix

// before
String cipher = rsa.encrypt(userInput.trim());

// after
String trimmed = userInput.trim();
if (trimmed.isEmpty()) {
    throw new IllegalArgumentException("Cannot encrypt an empty message");
}
String cipher = rsa.encrypt(trimmed);
Defensive patterns

Strategy: validation

Validate before calling

if (message == null || message.isEmpty()) {
    throw new IllegalArgumentException("Cannot RSA-encrypt an empty message");
}
String cipher = rsa.encrypt(message);

Prevention

When it happens

Trigger: Calling rsa.encrypt("") where rsa is an RSA instance; passing a string that was trimmed to empty before the call.

Common situations: User input field left blank but the encrypt path runs anyway; a string trimmed of whitespace becomes empty; a downstream variable was never assigned.

Related errors


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