jwtk/jjwt · error · IllegalArgumentException
The algorithm does not support shared secret keys.
Error message
The algorithm does not support shared secret keys.
What it means
Deprecated Keys.secretKeyFor(SignatureAlgorithm) throws IllegalArgumentException when the given algorithm is not a MAC (shared-secret) algorithm, i.e. an asymmetric algorithm like RS256/ES256 was requested as a secret key.
Solutions
- Only call secretKeyFor with HMAC algorithms: HS256, HS384, HS512
- Use Keys.keyPairFor(alg) instead for asymmetric algorithms
- Prefer the modern API: Jwts.SIG.HS256.key().build() (or the matching SIG instance)
- Check alg.name().startsWith("HS") before generating a secret key
Example fix
// before SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.RS256); // throws // after KeyPair kp = Keys.keyPairFor(SignatureAlgorithm.RS256); // asymmetric path SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256); // for HMAC
Defensive patterns
Strategy: type-guard
Validate before calling
if (!alg.name().startsWith("HS")) throw new IllegalArgumentException(alg + " is asymmetric; use keyPairFor"); Type guard
static boolean isHmac(io.jsonwebtoken.SignatureAlgorithm alg) {
return alg != null && alg.name().startsWith("HS");
} Try / catch
try {
SecretKey key = Keys.secretKeyFor(alg);
} catch (IllegalArgumentException e) {
KeyPair kp = Keys.keyPairFor(alg); // fall back to asymmetric generation
} Prevention
- Branch on algorithm family before generating keys: HS* -> secretKeyFor, others -> keyPairFor
- Prefer the modern Jwts.SIG.<Alg>.key()/keyPair() builders over deprecated helpers
- Write tests asserting key generation per SignatureAlgorithm constant
When it happens
Trigger: Calling Keys.secretKeyFor(SignatureAlgorithm.RS256) (or ES256, PS256, EdDSA) — any non-HMAC SignatureAlgorithm whose resolved SecureDigestAlgorithm is not a MacAlgorithm.
Common situations: Confusing HMAC vs RSA/ECDSA key generation; loops that iterate all SignatureAlgorithm values and generate keys indiscriminately; legacy code after migrating to JJWT 0.12.x APIs.
Understand the failure class
Background: "is deprecated and will be removed" — deprecation warnings for old API names, keywords, and options, and how to migrate before the removal release — this error's family across 29 libraries.
Related errors
- The algorithm does not support Key Pairs.
- bitLength must be an even multiple of 8
- derivedKeyBitLength may not exceed
- EC JWK x,y coordinates do not exist on elliptic curve
- ECPublicKey's ECPoint does not exist on elliptic curve
AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09).
Data as JSON: /api/errors/d64f742a3f403b04.
Report an issue: GitHub.
Appendix: source
Thrown at api/src/main/java/io/jsonwebtoken/security/Keys.java:145
* <td>512 bits (64 bytes)</td>
* </tr>
* </table>
*
* @param alg the {@code SignatureAlgorithm} to inspect to determine which key length to use.
* @return a new {@link SecretKey} instance suitable for use with the specified {@link SignatureAlgorithm}.
* @throws IllegalArgumentException for any input value other than {@link io.jsonwebtoken.SignatureAlgorithm#HS256},
* {@link io.jsonwebtoken.SignatureAlgorithm#HS384}, or {@link io.jsonwebtoken.SignatureAlgorithm#HS512}
* @deprecated since 0.12.0. Use your preferred {@link MacAlgorithm} instance's
* {@link MacAlgorithm#key() key()} builder method directly.
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public static SecretKey secretKeyFor(io.jsonwebtoken.SignatureAlgorithm alg) throws IllegalArgumentException {
Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
SecureDigestAlgorithm<?, ?> salg = Jwts.SIG.get().get(alg.name());
if (!(salg instanceof MacAlgorithm)) {
String msg = "The " + alg.name() + " algorithm does not support shared secret keys.";
throw new IllegalArgumentException(msg);
}
return ((MacAlgorithm) salg).key().build();
}
/**
* <p><b>Deprecation Notice</b></p>
*
* <p>As of JJWT 0.12.0, asymmetric key algorithm instances can generate KeyPairs of suitable strength
* for that specific algorithm by calling their {@code keyPair()} builder method directly. For example:</p>
*
* <blockquote><pre>
* Jwts.SIG.{@link Jwts.SIG#RS256 RS256}.keyPair().build();
* Jwts.SIG.{@link Jwts.SIG#RS384 RS384}.keyPair().build();
* Jwts.SIG.{@link Jwts.SIG#RS512 RS512}.keyPair().build();
* ... etc ...
* Jwts.SIG.{@link Jwts.SIG#ES512 ES512}.keyPair().build();</pre></blockquote>
*
* <p>Call those methods as needed instead of this static {@code keyPairFor} helper method - the returnedView on GitHub (pinned to fb71496164)