{"record":{"id":"e1d0ff16a0f4d272","repo":"denoland/deno","slug":"operation-not-supported-for-this-keytype","errorCode":null,"errorMessage":"operation not supported for this keytype","messagePattern":"operation not supported for this keytype","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/crypto/cipher.ts","lineNumber":778,"sourceCode":"  }\n}\n\nconst ENCRYPT_UNSUPPORTED_KEY_TYPES = new SafeSet([\n  \"rsa-pss\",\n  \"dsa\",\n  \"ec\",\n  \"ed25519\",\n  \"ed448\",\n  \"x25519\",\n  \"x448\",\n]);\n\nfunction checkUnsupportedKeyType(key) {\n  const keyType = isKeyObject(key)\n    ? key.asymmetricKeyType\n    : key?.key?.asymmetricKeyType;\n  if (keyType && SetPrototypeHas(ENCRYPT_UNSUPPORTED_KEY_TYPES, keyType)) {\n    throw new Error(\"operation not supported for this keytype\");\n  }\n}\n\nconst WEBCRYPTO_SHA_HYPHEN_RE = new SafeRegExp(\"^(sha)-(?!3-)\");\n\nfunction normalizeOaepHash(hash: unknown): string | undefined {\n  if (hash === undefined) return undefined;\n  if (typeof hash !== \"string\") {\n    throw new ERR_INVALID_ARG_TYPE(\"oaepHash\", \"string\", hash);\n  }\n  if (!hash) return undefined;\n  // Normalize to lowercase and strip WebCrypto-style hyphens\n  // (e.g. \"SHA-256\" -> \"sha256\") but keep sha3/sha512 sub-variants\n  // (e.g. \"sha3-256\", \"sha512-224\") intact.\n  const normalized = StringPrototypeReplace(\n    StringPrototypeToLowerCase(hash),\n    WEBCRYPTO_SHA_HYPHEN_RE,\n    \"$1\",","sourceCodeStart":760,"sourceCodeEnd":796,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/crypto/cipher.ts#L760-L796","documentation":"checkUnsupportedKeyType (cipher.ts:778) runs at the top of privateEncrypt, privateDecrypt, publicEncrypt and publicDecrypt. If the key's asymmetricKeyType is one of rsa-pss, dsa, ec, ed25519, ed448, x25519 or x448, it throws the plain Error 'operation not supported for this keytype'. These four operations are RSA primitives; the blocklist mirrors the key types OpenSSL refuses for RSA padding schemes.","triggerScenarios":"crypto.publicEncrypt(ed25519PublicKey, data); crypto.privateDecrypt({ key: ecKey }, ct); loading an rsa-pss key from a certificate and calling publicDecrypt; passing an x25519 key from an ECDH flow into RSA encrypt/decrypt.","commonSituations":"Mixing WebCrypto-generated Ed25519/EC key pairs with node:crypto RSA APIs; keys provisioned for signing reused for encryption; certificate-based key loading where the cert carries an EC or rsa-pss key.","solutions":["Use an RSA key pair (createPublicKey/createPrivateKey over PEM/PKCS#8) for publicEncrypt/privateDecrypt","For EC/Ed25519/Ed448 material, switch to the operations those keys support: sign/verify (ECDSA/EdDSA)","For x25519/x448, use crypto.diffieHellman({ privateKey, publicKey }) instead of encrypt/decrypt","If the key is rsa-pss, load an rsa (PKCS#1) key instead — rsa-pss is on the reject list"],"exampleFix":"// before\nconst { publicKey } = generateKeyPairSync('ed25519');\npublicEncrypt(publicKey, data); // operation not supported for this keytype\n\n// after\nconst { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 2048 });\nconst ct = publicEncrypt(publicKey, data);\nconst pt = privateDecrypt(privateKey, ct);","handlingStrategy":"type-guard","validationCode":"import { isKeyObject } from 'node:crypto';\nfunction assertRsaKeyForEncrypt(key) {\n  const t = isKeyObject(key) ? key.asymmetricKeyType : key?.key?.asymmetricKeyType;\n  if (t && t !== 'rsa') throw new TypeError(`key type ${t} is not usable for RSA encrypt/decrypt`);\n}","typeGuard":"function usableForRsaOps(key) {\n  const t = key?.asymmetricKeyType ?? key?.key?.asymmetricKeyType;\n  return t == null || t === 'rsa';\n}","tryCatchPattern":"try { publicEncrypt(key, data); } catch (e) { if (e.message === 'operation not supported for this keytype') { /* fetch/generate an RSA key instead */ } else throw e; }","preventionTips":["Assert asymmetricKeyType === 'rsa' before any public/private encrypt/decrypt call","Keep a key-usage registry: which key ids are for signing vs encryption vs key agreement","Unit-test key loading with the exact PEMs used in production"],"tags":["crypto","rsa","key-management","node-compat"],"backgroundTag":"wrong-key-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}