jwtk/jjwt · error · WeakKeyException
The specified SecretKey is not strong enough to be used…
Error message
The specified SecretKey is not strong enough to be used with JWT HMAC signature algorithms. The JWT specification requires HMAC keys to be >= 256 bits long. The specified key is ${bitLength} bits. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information. What it means
Error "The specified SecretKey is not strong enough to be used with JWT HMAC signature algorithms. The JWT specification requires HMAC keys to be >= 256 bits long. The specified key is ${bitLength} bits. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information." thrown in jwtk/jjwt.
Solutions
- Create a strong HMAC key with Keys.secretKeyFor(SignatureAlgorithm.HS256) or larger (384/512 bits).
- If the existing secret is too short, derive a longer key (e.g. via a KDF) to reach >=256 bits rather than padding naively.
- For testing only, use a fixed >=32-byte secret; do not weaken production requirements.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java:607 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/3bff82f1a056a714.
Report an issue: GitHub.
Appendix: source
Thrown at api/src/main/java/io/jsonwebtoken/SignatureAlgorithm.java:607
try {
encoded = secretKey.getEncoded();
bitLength = io.jsonwebtoken.lang.Arrays.length(encoded) * Byte.SIZE;
} finally {
Arrays.fill(encoded, (byte) 0);
}
for (SignatureAlgorithm alg : PREFERRED_HMAC_ALGS) {
// ensure compatibility check is based on key length. See https://github.com/jwtk/jjwt/issues/381
if (bitLength >= alg.minKeyLength) {
return alg;
}
}
String msg = "The specified SecretKey is not strong enough to be used with JWT HMAC signature " +
"algorithms. The JWT specification requires HMAC keys to be >= 256 bits long. The specified " +
"key is " + bitLength + " bits. See https://tools.ietf.org/html/rfc7518#section-3.2 for more " +
"information.";
throw new WeakKeyException(msg);
}
if (key instanceof RSAKey) {
RSAKey rsaKey = (RSAKey) key;
int bitLength = rsaKey.getModulus().bitLength();
if (bitLength >= 4096) {
RS512.assertValidSigningKey(key);
return RS512;
} else if (bitLength >= 3072) {
RS384.assertValidSigningKey(key);
return RS384;
} else if (bitLength >= RS256.minKeyLength) {
RS256.assertValidSigningKey(key);
return RS256;
}
View on GitHub (pinned to fb71496164)