{"record":{"id":"d74cbf4f3445ccd9","repo":"TheAlgorithms/Java","slug":"message-is-empty","errorCode":null,"errorMessage":"Message is empty","messagePattern":"Message is empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/ciphers/RSA.java","lineNumber":53,"sourceCode":"    /**\n     * Constructor that generates RSA keys with the specified number of bits.\n     *\n     * @param bits The bit length of the keys to be generated. Common sizes include 512, 1024, 2048, etc.\n     */\n    public RSA(int bits) {\n        generateKeys(bits);\n    }\n\n    /**\n     * Encrypts a text message using the RSA public key.\n     *\n     * @param message The plaintext message to be encrypted.\n     * @throws IllegalArgumentException If the message is empty.\n     * @return The encrypted message represented as a String.\n     */\n    public synchronized String encrypt(String message) {\n        if (message.isEmpty()) {\n            throw new IllegalArgumentException(\"Message is empty\");\n        }\n        return (new BigInteger(message.getBytes())).modPow(publicKey, modulus).toString();\n    }\n\n    /**\n     * Encrypts a BigInteger message using the RSA public key.\n     *\n     * @param message The plaintext message as a BigInteger.\n     * @return The encrypted message as a BigInteger.\n     */\n    public synchronized BigInteger encrypt(BigInteger message) {\n        return message.modPow(publicKey, modulus);\n    }\n\n    /**\n     * Decrypts an encrypted message (as String) using the RSA private key.\n     *\n     * @param encryptedMessage The encrypted message to be decrypted, represented as a String.","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/ciphers/RSA.java#L35-L71","documentation":"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.","triggerScenarios":"Calling rsa.encrypt(\"\") where rsa is an RSA instance; passing a string that was trimmed to empty before the call.","commonSituations":"User input field left blank but the encrypt path runs anyway; a string trimmed of whitespace becomes empty; a downstream variable was never assigned.","solutions":["Validate that the message is non-empty before calling encrypt.","Use the BigInteger overload rsa.encrypt(new BigInteger(message.getBytes())) only when you have a real value.","Treat an empty message as a no-op in your flow and skip encryption."],"exampleFix":"// before\nString cipher = rsa.encrypt(userInput.trim());\n\n// after\nString trimmed = userInput.trim();\nif (trimmed.isEmpty()) {\n    throw new IllegalArgumentException(\"Cannot encrypt an empty message\");\n}\nString cipher = rsa.encrypt(trimmed);","handlingStrategy":"validation","validationCode":"if (message == null || message.isEmpty()) {\n    throw new IllegalArgumentException(\"Cannot RSA-encrypt an empty message\");\n}\nString cipher = rsa.encrypt(message);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate message length before encrypting.","Treat empty input as a no-op rather than passing it to RSA.","Sanitize user input (trim) and check emptiness after trimming."],"tags":["cipher","rsa","validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}