{"record":{"id":"2bb64be992636602","repo":"jwtk/jjwt","slug":"invalid-secret-jwk-abstractjwk-alg-value-alg","errorCode":null,"errorMessage":"Invalid Secret JWK ${AbstractJwk.ALG} value '${alg.getId()}'. Secret JWKs may only be used with symmetric (secret) key algorithms.","messagePattern":"Invalid Secret JWK (.+?) value '(.+?)'\\. Secret JWKs may only be used with symmetric \\(secret\\) key algorithms\\.","errorType":"exception","errorClass":"MalformedKeyException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/security/SecretJwkFactory.java","lineNumber":95,"sourceCode":"            // Implementors note:  Don't print out any information about the `bytes` value itself - size,\n            // content, etc., as it is considered secret material:\n            String msg = \"Secret JWK \" + AbstractJwk.ALG + \" value is '\" + alg.getId() +\n                    \"', but the \" + DefaultSecretJwk.K + \" length is smaller than the \" + alg.getId() +\n                    \" minimum length of \" + Bytes.bitsMsg(requiredBitLen) +\n                    \" required by \" +\n                    \"[JWA RFC 7518, Section 3.2](https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2), \" +\n                    \"2nd paragraph: 'A key of the same size as the hash output or larger MUST be used with this \" +\n                    \"algorithm.'\";\n            throw new WeakKeyException(msg);\n        }\n    }\n\n    private static void assertSymmetric(Identifiable alg) {\n        if (alg instanceof MacAlgorithm || alg instanceof SecretKeyAlgorithm || alg instanceof AeadAlgorithm)\n            return; // valid\n        String msg = \"Invalid Secret JWK \" + AbstractJwk.ALG + \" value '\" + alg.getId() + \"'. Secret JWKs \" +\n                \"may only be used with symmetric (secret) key algorithms.\";\n        throw new MalformedKeyException(msg);\n    }\n\n    @Override\n    protected SecretJwk createJwkFromValues(JwkContext<SecretKey> ctx) {\n        ParameterReadable reader = new RequiredParameterReader(ctx);\n        final byte[] bytes = reader.get(DefaultSecretJwk.K);\n        SecretKey key;\n\n        String algId = ctx.getAlgorithm();\n        if (!Strings.hasText(algId)) { // optional per https://www.rfc-editor.org/rfc/rfc7517.html#section-4.4\n\n            // Here we try to infer the best type of key to create based on siguse and/or key length.\n            //\n            // AES requires 128, 192, or 256 bits, so anything larger than 256 cannot be AES, so we'll need to assume\n            // HMAC.\n            //\n            // Also, 256 bits works for either HMAC or AES, so we just have to choose one as there is no other\n            // RFC-based criteria for determining.  Historically, we've chosen AES due to the larger number of","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/security/SecretJwkFactory.java#L77-L113","documentation":"Thrown as MalformedKeyException when a SecretJwk declares an 'alg' that is not a symmetric algorithm (MAC, secret-key encryption, or AEAD). Secret JWKs represent symmetric key material and can only be associated with symmetric algorithms.","triggerScenarios":"Building a SecretJwk from values with 'alg' set to an asymmetric algorithm such as RS256, ES256, or RSA-OAEP instead of HS256/A128KW/A256GCM etc.","commonSituations":"Copy-pasting JWK templates across key types; misconfiguring algorithm headers after switching key material; typos or mixing up JWK 'kty' (oct) with 'alg' families.","solutions":["Set 'alg' to a symmetric algorithm (HS256/HS384/HS512, AxxxKW key-wrap, or GCM AEAD algorithms).","If the key is asymmetric, build the correct JWK type (EC or RSA JWK) instead of a SecretJwk.","Validate the alg value against Jwts.SIG / Jwts.ENC registry before building the JWK."],"exampleFix":"// before\nvalues.put(\"kty\", \"oct\");\nvalues.put(\"alg\", \"RS256\"); // asymmetric alg on secret JWK\n// after\nvalues.put(\"kty\", \"oct\");\nvalues.put(\"alg\", \"HS256\");","handlingStrategy":"validation","validationCode":"Object alg = Jwts.SIG.get().forId(algId);\nif (!(alg instanceof MacAlgorithm || alg instanceof SecretKeyAlgorithm || alg instanceof AeadAlgorithm)) throw new IllegalStateException(\"Secret JWK needs a symmetric alg\");","typeGuard":"boolean symmetricAlg(Object a) { return a instanceof MacAlgorithm || a instanceof SecretKeyAlgorithm || a instanceof AeadAlgorithm; }","tryCatchPattern":"try { /* build SecretJwk */ } catch (MalformedKeyException e) { throw new IllegalArgumentException(\"Use HS*/A*KW/GCM algs for oct (secret) JWKs\", e); }","preventionTips":["For kty=oct JWKs, only set alg to symmetric algorithms (HS256/384/512, AxxxKW, GCM).","Use asymmetric JWK types (RSA/EC) for RSA/EC algorithms.","Look up algorithms from Jwts.SIG/Jwts.ENC instead of hardcoding strings."],"tags":["jwk","secretkey","algorithm-mismatch","malformed-key"],"backgroundTag":"invalid-enum-value","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}