Getting Keycloak's public key

The keys are also directly on https://keycloak-server/auth/realms/my-realm, in a format directly exploitable with your code:

{
  "realm": "my-realm",
  "public_key": "MIIBI...",
  "token-service": "https://keycloak-server/auth/realms/my-realm/protocol/openid-connect",
  "account-service": "https://keycloak-server/auth/realms/my-realm/account",
  "tokens-not-before": 0
}

I'll found it also on:

  • open admin console
  • choose realm
  • choose Realm Settings
  • open tab 'Keys'
  • open tab 'active'
  • in the column 'Public keys' press 'Public Key enter image description here
  • a popup with the public key appears.

There is toIntegerBytes before base64 encode, so it is not just base64 decode. Try:

BigInteger modulus = new BigInteger(1, Base64.decodeBase64("n-value-here"));
BigInteger exponent = new BigInteger(1, Base64.decodeBase64("e-value-here"));

Tags:

Keycloak