jwtk/jjwt · error · io.jsonwebtoken.security.InvalidKeyException

Unrecognized EC key algorithm name.

Error message

Unrecognized EC key algorithm name.

What it means

EcSignatureAlgorithm.validateKey checks that the key's JCA algorithm name is one of the recognized EC names (e.g. 'EC', 'ECDSA'); otherwise the key cannot be assumed to be an EC key usable for ES256/ES384/ES512 signing. If findAlgorithm returns something outside KEY_ALG_NAMES, it throws this InvalidKeyException.

Source

Thrown at impl/src/main/java/io/jsonwebtoken/impl/security/EcSignatureAlgorithm.java:148

        this.OID = Assert.hasText(oid, "Invalid OID.");
        String curveName = "secp" + orderBitLength + "r1";
        this.KEY_PAIR_GEN_PARAMS = new ECGenParameterSpec(curveName);
        this.orderBitLength = orderBitLength;
        this.sigFieldByteLength = Bytes.length(this.orderBitLength);
        this.signatureByteLength = this.sigFieldByteLength * 2; // R bytes + S bytes = concat signature bytes
    }

    @Override
    public KeyPairBuilder keyPair() {
        return new DefaultKeyPairBuilder(ECCurve.KEY_PAIR_GENERATOR_JCA_NAME, this.KEY_PAIR_GEN_PARAMS)
                .random(Randoms.secureRandom());
    }

    @Override
    protected void validateKey(Key key, boolean signing) {
        super.validateKey(key, signing);
        if (!KEY_ALG_NAMES.contains(KeysBridge.findAlgorithm(key))) {
            throw new InvalidKeyException("Unrecognized EC key algorithm name.");
        }
        int size = KeysBridge.findBitLength(key);
        if (size < 0) return; // likely PKCS11 or HSM key, can't get the data we need
        int sigFieldByteLength = Bytes.length(size);
        int concatByteLength = sigFieldByteLength * 2;
        if (concatByteLength != this.signatureByteLength) {
            String msg = "The provided Elliptic Curve " + keyType(signing) +
                    " key size (aka order bit length) is " + Bytes.bitsMsg(size) + ", but the '" +
                    getId() + "' algorithm requires EC Keys with " + Bytes.bitsMsg(this.orderBitLength) +
                    " per [RFC 7518, Section 3.4](https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4).";
            throw new InvalidKeyException(msg);
        }
    }

    @Override
    protected byte[] doDigest(final SecureRequest<InputStream, PrivateKey> request) {
        return jca(request).withSignature(new CheckedFunction<Signature, byte[]>() {
            @Override

View on GitHub (pinned to fb71496164)

Solutions

  1. Supply an EC KeyPair (KeyPairGenerator.getInstance("EC") with a named curve) for ES* algorithms.
  2. Verify key.getAlgorithm() returns 'EC' or 'ECDSA' before signing.
  3. Match algorithm families: RSA keys with RS*/PS* algorithms, EC keys with ES* algorithms.
  4. For HSM keys with odd names, wrap or register the expected algorithm name, or use a key on a standard provider.

Example fix

// before
KeyPair rsaPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
Jwts.builder().signWith(rsaPair.getPrivate(), SignatureAlgorithm.ES256);
// after
KeyPairGenerator kg = KeyPairGenerator.getInstance("EC");
kg.initialize(new ECGenParameterSpec("secp256r1"));
Jwts.builder().signWith(kg.generateKeyPair().getPrivate(), SignatureAlgorithm.ES256);
Defensive patterns

Strategy: validation

Validate before calling

String alg = key.getAlgorithm();
if (!("EC".equalsIgnoreCase(alg) || "ECDSA".equalsIgnoreCase(alg))) {
    throw new IllegalArgumentException("ES* algorithms require an EC key, got: " + alg);
}

Type guard

boolean isEcKey(Key k) { return k instanceof java.security.interfaces.ECKey; }

Try / catch

try {
    jwt = Jwts.builder().signWith(priv, SignatureAlgorithm.ES256)...compact();
} catch (io.jsonwebtoken.security.InvalidKeyException e) {
    // match key family to algorithm family
}

Prevention

When it happens

Trigger: Using signWith(key, SignatureAlgorithm.ES256) (or a KeyPair with non-EC key) where the supplied key is an RSA, PSS, or custom-named key; keys from providers reporting unusual algorithm strings.

Common situations: Passing an RSA key pair to an EC signature algorithm; PKCS11/HSM keys whose algorithm name is provider-specific; accidental argument order mix-up in signWith calls.

Related errors


AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09). Data as JSON: /api/errors/8ba97f96aa419120. Report an issue: GitHub.