jwtk/jjwt · error · InvalidKeyException
Invalid RSA key algorithm name.
Error message
Invalid RSA key algorithm name.
What it means
This InvalidKeyException is thrown by DefaultRsaKeyAlgorithm.validate when the key passed to an RSA encryption key algorithm (RSA-OAEP, RSA1_5) does not report a Java Key algorithm name recognized as RSA. The library requires keys whose getAlgorithm() returns an RSA family name (e.g. 'RSA') to guarantee the key material is actually RSA before deriving encryption or decryption keys.
Source
Thrown at impl/src/main/java/io/jsonwebtoken/impl/security/DefaultRsaKeyAlgorithm.java:60
private static final int MIN_KEY_BIT_LENGTH = 2048;
public DefaultRsaKeyAlgorithm(String id, String jcaTransformationString) {
this(id, jcaTransformationString, null);
}
public DefaultRsaKeyAlgorithm(String id, String jcaTransformationString, AlgorithmParameterSpec spec) {
super(id, jcaTransformationString);
this.SPEC = spec; //can be null
}
private static String keyType(boolean encryption) {
return encryption ? "encryption" : "decryption";
}
protected void validate(Key key, boolean encryption) { // true = encryption, false = decryption
if (!RsaSignatureAlgorithm.isRsaAlgorithmName(key)) {
throw new InvalidKeyException("Invalid RSA key algorithm name.");
}
if (RsaSignatureAlgorithm.isPss(key)) {
String msg = "RSASSA-PSS keys may not be used for " + keyType(encryption) +
", only digital signature algorithms.";
throw new InvalidKeyException(msg);
}
int size = KeysBridge.findBitLength(key);
if (size < 0) return; // can't validate size: material or length not available (e.g. PKCS11 or HSM)
if (size < MIN_KEY_BIT_LENGTH) {
String id = getId();
String section = id.startsWith("RSA1") ? "4.2" : "4.3";
String msg = "The RSA " + keyType(encryption) + " key size (aka modulus bit length) is " + size +
" bits which is not secure enough for the " + id + " algorithm. " +
"The JWT JWA Specification (RFC 7518, Section " + section + ") states that RSA keys MUST " +
"have a size >= " + MIN_KEY_BIT_LENGTH + " bits. See " +
"https://www.rfc-editor.org/rfc/rfc7518.html#section-" + section + " for more information.";View on GitHub (pinned to fb71496164)
Solutions
- Pass an RSA PublicKey (encryption) or PrivateKey (decryption) generated with KeyPairGenerator.getInstance("RSA").
- Verify key.getAlgorithm() returns an RSA name before handing the key to the builder/parser.
- If using a custom provider key, wrap or convert it to a standard RSAKey via KeyFactory.getInstance("RSA").
- Check that you are not accidentally supplying the MAC/signature key as the encryption key.
Example fix
// before
Jwts.builder().encryptWith(aesSecretKey, Jwts.KEY.RSA_OAEP)...
// after
KeyPairGenerator kg = KeyPairGenerator.getInstance("RSA");
kg.initialize(2048);
KeyPair kp = kg.generateKeyPair();
Jwts.builder().encryptWith(kp.getPublic(), Jwts.KEY.RSA_OAEP)... Defensive patterns
Strategy: validation
Validate before calling
if (!"RSA".equalsIgnoreCase(key.getAlgorithm()) && !key.getAlgorithm().toUpperCase().contains("RSA")) {
throw new IllegalArgumentException("Expected an RSA key for RSA key-encryption, got: " + key.getAlgorithm());
} Type guard
boolean isRsaKey(Key k) { return k instanceof java.security.interfaces.RSAKey; } Try / catch
try {
jwt = Jwts.builder().encryptWith(pub, Jwts.KEY.RSA_OAEP)...compact();
} catch (io.jsonwebtoken.security.InvalidKeyException e) {
// log key.getAlgorithm() and use an RSA key
} Prevention
- Always pass RSA keys to RSA-OAEP/RSA1_5 algorithms
- Check key.getAlgorithm() before use
- Keep signing keys and encryption keys clearly separated in config
When it happens
Trigger: Calling getEncryptionKey or getDecryptionKey via Jwts.builder().encryptWith(...) or parser decryptWith(...) with a SecretKey, EC key, or other non-RSA key while the JWA algorithm is an RSA key algorithm; or a custom/foreign Provider key whose algorithm name is null or non-standard.
Common situations: Passing an AES SecretKey where an RSA PublicKey/PrivateKey is expected; loading keys from keystores/PKCS11 with unusual algorithm naming; mixing up signature keys with encryption keys in JWE code.
Related errors
- RSASSA-PSS keys may not be used for ${keyType}, only digital
- Unexpected content JWE.
- Unexpected Claims JWE.
- The '${id}' algorithm requires keys with a length of ${bitsM
- The '${id}' algorithm requires ${type} with a length of ${bi
AI-assisted analysis of jwtk/jjwt@fb71496164 (2026-09-09).
Data as JSON: /api/errors/ea27242617f14bbf.
Report an issue: GitHub.