{"record":{"id":"ea27242617f14bbf","repo":"jwtk/jjwt","slug":"invalid-rsa-key-algorithm-name","errorCode":null,"errorMessage":"Invalid RSA key algorithm name.","messagePattern":"Invalid RSA key algorithm name\\.","errorType":"validation","errorClass":"InvalidKeyException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/security/DefaultRsaKeyAlgorithm.java","lineNumber":60,"sourceCode":"    private static final int MIN_KEY_BIT_LENGTH = 2048;\n\n    public DefaultRsaKeyAlgorithm(String id, String jcaTransformationString) {\n        this(id, jcaTransformationString, null);\n    }\n\n    public DefaultRsaKeyAlgorithm(String id, String jcaTransformationString, AlgorithmParameterSpec spec) {\n        super(id, jcaTransformationString);\n        this.SPEC = spec; //can be null\n    }\n\n    private static String keyType(boolean encryption) {\n        return encryption ? \"encryption\" : \"decryption\";\n    }\n\n    protected void validate(Key key, boolean encryption) { // true = encryption, false = decryption\n\n        if (!RsaSignatureAlgorithm.isRsaAlgorithmName(key)) {\n            throw new InvalidKeyException(\"Invalid RSA key algorithm name.\");\n        }\n\n        if (RsaSignatureAlgorithm.isPss(key)) {\n            String msg = \"RSASSA-PSS keys may not be used for \" + keyType(encryption) +\n                    \", only digital signature algorithms.\";\n            throw new InvalidKeyException(msg);\n        }\n\n        int size = KeysBridge.findBitLength(key);\n        if (size < 0) return; // can't validate size: material or length not available (e.g. PKCS11 or HSM)\n        if (size < MIN_KEY_BIT_LENGTH) {\n            String id = getId();\n            String section = id.startsWith(\"RSA1\") ? \"4.2\" : \"4.3\";\n            String msg = \"The RSA \" + keyType(encryption) + \" key size (aka modulus bit length) is \" + size +\n                    \" bits which is not secure enough for the \" + id + \" algorithm. \" +\n                    \"The JWT JWA Specification (RFC 7518, Section \" + section + \") states that RSA keys MUST \" +\n                    \"have a size >= \" + MIN_KEY_BIT_LENGTH + \" bits. See \" +\n                    \"https://www.rfc-editor.org/rfc/rfc7518.html#section-\" + section + \" for more information.\";","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultRsaKeyAlgorithm.java#L42-L78","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nJwts.builder().encryptWith(aesSecretKey, Jwts.KEY.RSA_OAEP)...\n// after\nKeyPairGenerator kg = KeyPairGenerator.getInstance(\"RSA\");\nkg.initialize(2048);\nKeyPair kp = kg.generateKeyPair();\nJwts.builder().encryptWith(kp.getPublic(), Jwts.KEY.RSA_OAEP)...","handlingStrategy":"validation","validationCode":"if (!\"RSA\".equalsIgnoreCase(key.getAlgorithm()) && !key.getAlgorithm().toUpperCase().contains(\"RSA\")) {\n    throw new IllegalArgumentException(\"Expected an RSA key for RSA key-encryption, got: \" + key.getAlgorithm());\n}","typeGuard":"boolean isRsaKey(Key k) { return k instanceof java.security.interfaces.RSAKey; }","tryCatchPattern":"try {\n    jwt = Jwts.builder().encryptWith(pub, Jwts.KEY.RSA_OAEP)...compact();\n} catch (io.jsonwebtoken.security.InvalidKeyException e) {\n    // log key.getAlgorithm() and use an RSA key\n}","preventionTips":["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"],"tags":["java","jjwt","rsa","invalid-key","jwe"],"backgroundTag":"invalid-key-algorithm","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}