{"record":{"id":"6cebaafab7a8420b","repo":"gchq/CyberChef","slug":"couldn-t-encrypt-message-with-provided-public-key","errorCode":null,"errorMessage":"Couldn't encrypt message with provided public key: ${err}","messagePattern":"Couldn't encrypt message with provided public key: (.+?)","errorType":"exception","errorClass":"OperationError","httpStatus":null,"severity":"error","filePath":"src/core/operations/PGPEncrypt.mjs","lineNumber":71,"sourceCode":"     * @throws {OperationError} if failed private key import or failed encryption\n     */\n    async run(input, args) {\n        const plaintextMessage = input,\n            plainPubKey = args[0];\n        let encryptedMessage;\n\n        if (!plainPubKey) throw new OperationError(\"Enter the public key of the recipient.\");\n\n        const key = await importPublicKey(plainPubKey);\n\n        try {\n            encryptedMessage = await promisify(kbpgp.box)({\n                \"msg\": plaintextMessage,\n                \"encrypt_for\": key,\n                \"asp\": ASP\n            });\n        } catch (err) {\n            throw new OperationError(`Couldn't encrypt message with provided public key: ${err}`);\n        }\n\n        return encryptedMessage.toString();\n    }\n\n}\n\nexport default PGPEncrypt;\n","sourceCodeStart":53,"sourceCodeEnd":80,"githubUrl":"https://github.com/gchq/CyberChef/blob/4290ea753912378913b1f3f54e0fc5720afeda5d/src/core/operations/PGPEncrypt.mjs#L53-L80","documentation":"Catch-all around kbpgp.box in PGPEncrypt.run. The public key imported fine (importPublicKey succeeded) but the encryption call failed. The appended err holds the underlying kbpgp cause.","triggerScenarios":"Public key is structurally valid but has no encryption-capable subkey (signing-only primary); extremely large message causing an internal failure; kbpgp rejects the key's algorithm or packet structure.","commonSituations":"Recipient public key lacks an encryption subkey; very large input; key from an incompatible OpenPGP implementation that imports but cannot encrypt.","solutions":["Use a recipient public key that has an encryption subkey.","Reduce the message size and retry.","Re-armour and validate the public key."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (!/-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(plainPubKey)) {\n    throw new Error('Recipient argument is not an armoured PGP public key.');\n}","typeGuard":"const isArmouredPublicKey = (s) =>\n    typeof s === 'string' && /-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(s);","tryCatchPattern":"try {\n    cipher = await chef.PGPEncrypt(msg, [pubKey]);\n} catch (e) {\n    if (/Couldn't encrypt/.test(e.message)) { /* inspect suffix for kbpgp cause */ }\n    else throw e;\n}","preventionTips":["Use a public key that has an encryption subkey.","Validate the armoured public key before encrypting.","Keep message size reasonable."],"tags":["pgp","kbpgp","encryption","cryptography"],"backgroundTag":null,"analyzedSha":"4290ea753912378913b1f3f54e0fc5720afeda5d","analyzedAt":"2026-08-13T06:05:50.210Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}