{"record":{"id":"8d529c65bc379107","repo":"apache/pulsar","slug":"failed-to-encrypt-payload","errorCode":null,"errorMessage":"Failed to encrypt payload","messagePattern":"Failed to encrypt payload","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"pulsar-broker/src/main/java/org/apache/pulsar/client/impl/RawBatchMessageContainerImpl.java","lineNumber":74,"sourceCode":"        this.compressor = new CompressionCodecNone();\n    }\n\n    private ByteBuf encrypt(ByteBuf compressedPayload) {\n        if (msgCrypto == null) {\n            return compressedPayload;\n        }\n        int maxSize = msgCrypto.getMaxOutputSize(compressedPayload.readableBytes());\n        ByteBuf encryptedPayload = allocator.buffer(maxSize);\n        ByteBuffer targetBuffer = encryptedPayload.nioBuffer(0, maxSize);\n\n        try {\n            msgCrypto.encrypt(encryptionKeys, cryptoKeyReader, () -> messageMetadata,\n                    compressedPayload.nioBuffer(), targetBuffer);\n        } catch (PulsarClientException e) {\n            encryptedPayload.release();\n            compressedPayload.release();\n            discard(e);\n            throw new RuntimeException(\"Failed to encrypt payload\", e);\n        }\n        encryptedPayload.writerIndex(targetBuffer.remaining());\n        compressedPayload.release();\n        return encryptedPayload;\n    }\n\n    @Override\n    public ProducerImpl.OpSendMsg createOpSendMsg() {\n        throw new UnsupportedOperationException();\n    }\n\n    /**\n     * Sets a CryptoKeyReader instance to encrypt batched messages during serialization, `toByteBuf()`.\n     * @param cryptoKeyReader a CryptoKeyReader instance\n     */\n    public void setCryptoKeyReader(CryptoKeyReader cryptoKeyReader) {\n        this.cryptoKeyReader = cryptoKeyReader;\n    }","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-broker/src/main/java/org/apache/pulsar/client/impl/RawBatchMessageContainerImpl.java#L56-L92","documentation":"RawBatchMessageContainerImpl.encrypt wraps a message-encryption failure: the MessageCrypto encrypt call threw (bad key, wrong max output size, or crypto provider error) while encrypting the compressed batch payload.","triggerScenarios":"Thrown at pulsar-broker/src/main/java/org/apache/pulsar/client/impl/RawBatchMessageContainerImpl.java:74 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the encryption public key is valid and current","Ensure the allocated buffer size (getMaxOutputSize) is sufficient and the crypto provider is configured"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}