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

  1. Confirm the block begins with '-----BEGIN PGP PUBLIC KEY BLOCK-----'.
  2. Re-export the public key from a trusted keyring (gpg --armor --export).
  3. Strip and re-flow the armor to RFC 4880 line lengths (max 76 chars per base64 line).
  4. Trim trailing whitespace and newlines from the armored text before importing.
  5. 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

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


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