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 3.2) states that keys used with ${name} MUST have a size >= ${minKeyLength} bits (the key size must be greater than or equal to the hash output size).  Consider using the ${Keys} class's 'secretKeyFor(SignatureAlgorithm.${name})' method to create a key guaranteed to be secure enough for ${name}.  See https://tools.ietf.org/html/rfc7518#section-3.2 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 3.2) states that keys used with ${name} MUST have a size >= ${minKeyLength} bits (the key size must be greater than or equal to the hash output size). Consider using the ${Keys} class's 'secretKeyFor(SignatureAlgorithm.${name})' method to create a key guaranteed to be secure enough for ${name}. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information." thrown in jwtk/jjwt.

Source

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

                        !HS512.jcaName.equalsIgnoreCase(alg) &&
                        !HS256.pkcs12Name.equals(alg) &&
                        !HS384.pkcs12Name.equals(alg) &&
                        !HS512.pkcs12Name.equals(alg)) {
                    throw new InvalidKeyException("The " + keyType(signing) + " key's algorithm '" + alg +
                            "' does not equal a valid HmacSHA* algorithm name and cannot be used with " + name() + ".");
                }

                int size = encoded.length * 8; //size in bits
                if (size < this.minKeyLength) {
                    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 3.2) states that keys used with " + name() + " MUST have a " +
                            "size >= " + minKeyLength + " bits (the key size must be greater than or equal to the hash " +
                            "output size).  Consider using the " + Keys.class.getName() + " class's " +
                            "'secretKeyFor(SignatureAlgorithm." + name() + ")' method to create a key guaranteed to be " +
                            "secure enough for " + name() + ".  See " +
                            "https://tools.ietf.org/html/rfc7518#section-3.2 for more information.";
                    throw new WeakKeyException(msg);
                }
            } finally {
                if (encoded != null) {
                    Arrays.fill(encoded, (byte) 0);
                }
            }

        } else { //EC or RSA

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

            if (isEllipticCurve()) {

View on GitHub (pinned to fb71496164)

Solutions

  1. Generate a sufficiently large HMAC key with Keys.secretKeyFor(SignatureAlgorithm.HS256/HS384/HS512) — 256/384/512 bits respectively.
  2. If using an existing secret, derive or extend it to at least the algorithm's minKeyLength bits (HS256 needs >=256 bits).
  3. Downgrade to a smaller HS* algorithm (e.g. HS256) only if the key length satisfies that algorithm's requirement; never truncate a strong key to fit a weak one.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java:393 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/f9cdf5c1292cf7cd. Report an issue: GitHub.