TheAlgorithms/Java · error · IllegalArgumentException

Key must not be empty

Error message

Key must not be empty

What it means

XORCipher.encrypt(String, String) requires a non-empty key because it XORs each plaintext byte against repeating key bytes via xor(plainTextBytes, keyBytes). With an empty key array the XOR cycle is undefined, so the guard fails fast with a descriptive message.

Source

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

        byte[] outputBytes = new byte[inputBytes.length];
        for (int i = 0; i < inputBytes.length; ++i) {
            outputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]);
        }
        return outputBytes;
    }

    /**
     * Encrypts the given plaintext using the XOR cipher with the specified key.
     * The result is a hexadecimal-encoded string representing the ciphertext.
     *
     * @param plainText The input plaintext to encrypt.
     * @param key The encryption key.
     * @throws IllegalArgumentException if the key is empty.
     * @return A hexadecimal string representing the encrypted text.
     */
    public static String encrypt(final String plainText, final String key) {
        if (key.isEmpty()) {
            throw new IllegalArgumentException("Key must not be empty");
        }

        byte[] plainTextBytes = plainText.getBytes(CS_DEFAULT);
        byte[] keyBytes = key.getBytes(CS_DEFAULT);
        byte[] xorResult = xor(plainTextBytes, keyBytes);
        return HexFormat.of().formatHex(xorResult);
    }

    /**
     * Decrypts the given ciphertext (in hexadecimal format) using the XOR cipher
     * with the specified key. The result is the original plaintext.
     *
     * @param cipherText The hexadecimal string representing the encrypted text.
     * @param key The decryption key (must be the same as the encryption key).
     * @throws IllegalArgumentException if the key is empty.
     * @return The decrypted plaintext.
     */
    public static String decrypt(final String cipherText, final String key) {

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Provide a non-empty key string such as "secret".
  2. Validate the key is non-empty before encrypting.
  3. Load the key through a helper that throws if the source value is missing.

Example fix

// before
String ct = XORCipher.encrypt(text, key);

// after
if (key == null || key.isEmpty()) {
    throw new IllegalStateException("XOR key not provided");
}
String ct = XORCipher.encrypt(text, key);
Defensive patterns

Strategy: validation

Validate before calling

if (key == null || key.isEmpty()) {
    throw new IllegalStateException("XOR key must be non-empty");
}
String ct = XORCipher.encrypt(plainText, key);

Prevention

When it happens

Trigger: Calling XORCipher.encrypt(text, ""); passing a key string that resolved to empty.

Common situations: Key not loaded from its source (env var unset, config field blank); key defaulted to empty string; key cleared during a refactor.

Related errors


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