3

I am using JWT. To encrypt the token I am using the HS512 signature algorithm with base64EncodedSecretKey in Java. After I got the token I am able to decrypt the token without knowing the secret key. How is this possible? Is there anything wrong with my token?

String JWT = Jwts.builder()
  .signWith(SignatureAlgorithm.HS512, SECRET)
  .setSubject(username)
  .setExpiration(new Date(System.currentTimeMillis() + EXPIRATIONTIME))
  .setAudience("ADMIN")
  .compact();

Here JWT is my token and I set the secret key by calling this method:

signWith(SignatureAlgorithm.HS512, SECRET)

String SECRET is my key.

But when I make a request with correct user_name and password through postman I received this token in the header:

eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTUyMjkyMjAzOSwiYXVkIjoiQURNSU4ifQ.Wye52RTz8P3_7gPxZnJHOArA-ixaNHhQEcfoiAELu_56WXmMcZEAOlUyqP8yI0CWOZ4deXFRcP6azBpZpwNt-w

When I decrypt it I can view the token data:

{
  alg: "HS512"
}.
{
   sub: "admin",
   exp: 1522922039,
   aud: "ADMIN"
}

So my Question is: How is it possible to decrypt the JWT without knowing my secret key?

John
  • 33
  • 1
  • 5

2 Answers2

13

To encrypt the token I am using the HS512 signature algorithm [...]

No, you are not encrypting the token. You are signing it.

After I got the token I am able to decrypt the token without knowing the secret key [...]

No, you are not decrypting the token payload. You are decoding it.

The token payload is a JSON string encoded as Base64 and no keys are required to decode it.


JSON Web Token (JWT) is a open standard that defines a compact and self-contained way for securely transmitting information between parties as a JSON object.

JWT is a generic name for the following types of token:

  • JSON Web Signature (JWS): The payload is encoded and signed so the integrity of the claims can be verified.

  • JSON Web Encryption (JWE): They payload is encrypted so the claims are hidden from other parties.

JWT, JWS and JWE
The image was extracted from this page.

J. Campbell
  • 354
  • 1
  • 2
  • 15
cassiomolin
  • 101,346
  • 24
  • 214
  • 283
2

JWT Token has 3 parts separated by .(dots). First part Header, 2nd is Payload and 3rd is Signature. Header and Payload is encoded using Base64Url(little variant of Base64). This can be decoded by any one. Important part is signature. To sign the token a hash algorithm is used which takes header , payload and one secret (password) as parameter and generate a hash value. This hash is appended in jwt token as 3rd field plays as signature.

Server receive this token back as "Authorization Bearer" Header. It calculate hash using header, payload and secret password. If this calculated hash matches with hash present in token, gives assurance that no one has changed the token while travelling. If doesn't match it invalidates the token. Secret plays very important role. If this is stolen, people can generate same signature. Reference - https://blog.angular-university.io/angular-jwt/

Manoj K D
  • 21
  • 1