gchq/CyberChef · error · OperationError
Could not import public key: ${err}
Error message
Could not import public key: ${err} What it means
importPublicKey wraps kbpgp.KeyManager.import_from_armored_pgp in try/catch and rethrows any failure as `Could not import public key: ${err}`. Public keys have no passphrase step, so this fires purely on parse/format errors from kbpgp.
Source
Thrown at src/core/lib/PGP.mjs:116
}
/**
* Import public key
*
* @param {string} publicKey
* @returns {Object}
*/
export async function importPublicKey (publicKey) {
try {
const key = await promisify(kbpgp.KeyManager.import_from_armored_pgp)({
armored: publicKey,
opts: {
"no_check_keys": true
}
});
return key;
} catch (err) {
throw new OperationError(`Could not import public key: ${err}`);
}
}
View on GitHub (pinned to 4290ea7539)
Solutions
- Confirm the block begins with '-----BEGIN PGP PUBLIC KEY BLOCK-----'.
- Re-export the public key from a trusted keyring (gpg --armor --export).
- Strip and re-flow the armor to RFC 4880 line lengths (max 76 chars per base64 line).
- Trim trailing whitespace and newlines from the armored text before importing.
- Inspect the wrapped `err` for kbpgp's specific parse failure reason.
Example fix
// before
const pub = await PGP.importPublicKey(maybePrivateKeyOrMangled);
// after
const cleaned = armored.trim();
if (!cleaned.includes('BEGIN PGP PUBLIC KEY BLOCK'))
throw new Error('Not an OpenPGP public key block');
const pub = await PGP.importPublicKey(cleaned); Defensive patterns
Strategy: try-catch
Validate before calling
function looksLikeArmoredPublicKey(s) {
return /-----BEGIN PGP PUBLIC KEY BLOCK-----/.test(s) &&
/-----END PGP PUBLIC KEY BLOCK-----/.test(s);
}
if (!looksLikeArmoredPublicKey(armor.trim())) {
throw new Error('Input is not an OpenPGP armored public key');
} Try / catch
try {
return await PGP.importPublicKey(armor.trim());
} catch (e) {
const cause = String(e.message).replace(/^Could not import public key: /, '');
throw new Error('Failed to parse public key: ' + cause);
} Prevention
- Confirm the armor begins with BEGIN PGP PUBLIC KEY BLOCK.
- Do not paste a private key block where a public key is expected.
- Re-export public keys with gpg --armor --export to ensure RFC 4880 compliance.
- Re-flow base64 lines to <=76 chars and strip trailing whitespace.
When it happens
Trigger: Calling importPublicKey with a malformed armored public key, a private key block, an SSH/SSL public key, or armor that has been mangled (line wrapping stripped, header corrupted).
Common situations: Pasting a private key block where a public key was expected; armor copied from email client that re-wrapped long lines; missing or wrong armor headers (BEGIN PGP PUBLIC KEY BLOCK); key generated by a non-RFC 4880 compliant tool; trailing whitespace.
Related errors
- Could not import private key: ${err}
- Did not provide passphrase with locked private key.
- Enter the private key of the recipient.
- Couldn't decrypt message with provided private key: ${err}
- Enter the public key of the signer.
AI-assisted analysis of gchq/CyberChef@4290ea7539 (2026-08-13).
Data as JSON: /api/errors/3da76510e4443389.
Report an issue: GitHub.