gchq/CyberChef · error · OperationError

Couldn't encrypt message with provided public key: ${err}

Error message

Couldn't encrypt message with provided public key: ${err}

What it means

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.

Source

Thrown at src/core/operations/PGPEncrypt.mjs:71

     * @throws {OperationError} if failed private key import or failed encryption
     */
    async run(input, args) {
        const plaintextMessage = input,
            plainPubKey = args[0];
        let encryptedMessage;

        if (!plainPubKey) throw new OperationError("Enter the public key of the recipient.");

        const key = await importPublicKey(plainPubKey);

        try {
            encryptedMessage = await promisify(kbpgp.box)({
                "msg": plaintextMessage,
                "encrypt_for": key,
                "asp": ASP
            });
        } catch (err) {
            throw new OperationError(`Couldn't encrypt message with provided public key: ${err}`);
        }

        return encryptedMessage.toString();
    }

}

export default PGPEncrypt;

View on GitHub (pinned to 4290ea7539)

Solutions

  1. Use a recipient public key that has an encryption subkey.
  2. Reduce the message size and retry.
  3. Re-armour and validate the public key.
Defensive patterns

Strategy: try-catch

Validate before calling

if (!/-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(plainPubKey)) {
    throw new Error('Recipient argument is not an armoured PGP public key.');
}

Type guard

const isArmouredPublicKey = (s) =>
    typeof s === 'string' && /-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(s);

Try / catch

try {
    cipher = await chef.PGPEncrypt(msg, [pubKey]);
} catch (e) {
    if (/Couldn't encrypt/.test(e.message)) { /* inspect suffix for kbpgp cause */ }
    else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: Recipient public key lacks an encryption subkey; very large input; key from an incompatible OpenPGP implementation that imports but cannot encrypt.

Related errors


AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13). Data as JSON: /api/errors/6cebaafab7a8420b. Report an issue: GitHub.