
dac3n (dac3n) asked a question.
Hi All,
I am trying to generate JWT token in my standalone Java code by following the guideline provided in https://developer.okta.com/docs/guides/build-self-signed-jwt/java/main/*build-a-jwt-with-a-private-key
However while generating the JWT using the private key I am getting below exception:
Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.
Below is code snippet:
public void getJWT1() throws Exception
{
Gson gson = new Gson();
Map<String,String> keyMap = new HashMap<>();
keyMap.put("d", "Value");
keyMap.put("q","Value");
keyMap.put("p", "Value");
keyMap.put("dp", "Value");
keyMap.put("dq", "Value");
keyMap.put("qi", "Value");
keyMap.put("kty", "RSA");
keyMap.put("e", "AQAB");
keyMap.put("kid", "sVdxUhe6FJ1d0uDp4-qPqkCbrR1SnZUmXXFiVhKP4rw");
keyMap.put("n", "Value");
String key = gson.toJson(keyMap);
Instant now = Instant.now();
String UUID="avbch";
String jwt = Jwts.builder()
.setAudience("https://tenent-url/oauth2/default/v1/token")
.setIssuedAt(Date.from(now))
.setExpiration(Date.from(now.plus(5L, ChronoUnit.MINUTES)))
.setIssuer("Clinet Id")
.setSubject("Client id")
.setId(UUID)
.signWith(SignatureAlgorithm.RS256, key)
.compact();
System.out.println(jwt);
Also when I try to generate using client id and secret using same article I get below exception
Exception in thread "main" java.lang.NoSuchMethodError: io.jsonwebtoken.SignatureAlgorithm.getMinKeyLength()I
at io.jsonwebtoken.security.Keys.hmacShaKeyFor(Keys.java:69)
Let me know if anyone can suggest the changes to make this working.
Regards
Rahul Jha

Hello @dac3n (dac3n)
Thanks for posting.
Please refer to the following threads:
https://stackoverflow.com/questions/50201237/base64-encoded-key-bytes-may-only-be-specified-for-hmac-signatures
https://devforum.okta.com/t/verify-token-signature/1444
For more specific details applied to your specific use case feel free to post this question on our Okta Developer Forums: https://devforum.okta.com, this is a place for the Okta developer community to interact.
Let us know if this helps you.
Daniela Chavarria.
Okta Inc.