jwtk/jjwt · error · WeakKeyException

The ${keyType} key's size is ${size} bits which is not secur

Error message

The ${keyType} key's size is ${size} bits which is not secure enough for the ${name} algorithm.  The JWT JWA Specification (RFC 7518, Section ${section}) states that keys used with ${name} MUST have a size >= ${minKeyLength} bits.  Consider using the ${Keys} class's 'keyPairFor(SignatureAlgorithm.${name})' method to create a key pair guaranteed to be secure enough for ${name}.  See https://tools.ietf.org/html/rfc7518#section-${section} for more information.

What it means

Error "The ${keyType} key's size is ${size} bits which is not secure enough for the ${name} algorithm. The JWT JWA Specification (RFC 7518, Section ${section}) states that keys used with ${name} MUST have a size >= ${minKeyLength} bits. Consider using the ${Keys} class's 'keyPairFor(SignatureAlgorithm.${name})' method to create a key pair guaranteed to be secure enough for ${name}. See https://tools.ietf.org/html/rfc7518#section-${section} for more information." thrown in jwtk/jjwt.

Source

Thrown at api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java:451

                if (!(key instanceof RSAKey)) {
                    String msg = familyName + " " + keyType(signing) + " keys must be RSAKey instances.";
                    throw new InvalidKeyException(msg);
                }

                RSAKey rsaKey = (RSAKey) key;
                int size = rsaKey.getModulus().bitLength();
                if (size < this.minKeyLength) {

                    String section = name().startsWith("P") ? "3.5" : "3.3";

                    String msg = "The " + keyType(signing) + " key's size is " + size + " bits which is not secure " +
                            "enough for the " + name() + " algorithm.  The JWT JWA Specification (RFC 7518, Section " +
                            section + ") states that keys used with " + name() + " MUST have a size >= " +
                            this.minKeyLength + " bits.  Consider using the " + Keys.class.getName() + " class's " +
                            "'keyPairFor(SignatureAlgorithm." + name() + ")' method to create a key pair guaranteed " +
                            "to be secure enough for " + name() + ".  See " +
                            "https://tools.ietf.org/html/rfc7518#section-" + section + " for more information.";
                    throw new WeakKeyException(msg);
                }
            }
        }
    }

    /**
     * Returns the recommended signature algorithm to be used with the specified key according to the following
     * heuristics:
     *
     * <table>
     * <caption>Key Signature Algorithm</caption>
     * <thead>
     * <tr>
     * <th>If the Key is a:</th>
     * <th>And:</th>
     * <th>With a key size of:</th>
     * <th>The returned SignatureAlgorithm will be:</th>
     * </tr>

View on GitHub (pinned to fb71496164)

Solutions

  1. Generate an RSA key pair of at least 2048 bits with Keys.keyPairFor(SignatureAlgorithm.RS256); 3072 bits for RS384/PS384 and 4096 for RS512/PS512.
  2. Replace any 1024-bit legacy RSA keys with 2048+-bit keys and re-issue tokens.
  3. If a smaller key must be kept, choose an RSA algorithm whose minKeyLength it satisfies (none allow <2048 for JWT use in practice).
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java:451 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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