Creating and Parsing a asymmetric key signed JWT containing payload

In this post under JJWT, I will show with example how to create and parse asymmetric key signed JWT.

Note: The code uses the old version of JJWT. I will update the code using newer version in near future

The JWT can contain claims or payload.

For our example we will use a JWT containing payload.

Below is the complete code for your reference

Main Code


1  package defaultPackage;
2  import java.security.KeyPair;
3  import java.security.PrivateKey;
4  import java.security.PublicKey;
5  import java.util.HashMap;
6  import java.util.Map;
7  
8  import io.jsonwebtoken.Header;
9  import io.jsonwebtoken.Jws;
10 import io.jsonwebtoken.JwtBuilder;
11 import io.jsonwebtoken.JwtParser;
12 import io.jsonwebtoken.JwtParserBuilder;
13 import io.jsonwebtoken.Jwts;
14 import io.jsonwebtoken.SignatureAlgorithm;
15 import io.jsonwebtoken.security.Keys;
16 
17 public class Example10 {
18     public static void main(String[] args) {
19         KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS512);
20         Example10 example10 = new Example10();
21         String data = example10.toJWTString(keyPair.getPrivate());
22         System.out.println(data);
23         Jws jws = example10.toJWS(keyPair.getPublic(), data);
24         System.out.println(jws.getBody());
25         System.out.println(jws.getSignature());
26         System.out.println(jws.getHeader());
27     }
28 
29     public String toJWTString(PrivateKey privateKey) {
30         JwtBuilder jwtBuilder = Jwts.builder();
31         jwtBuilder.signWith(privateKey);
32         
33         Map headerMap = new HashMap<>();
34         headerMap.put("typ", Header.JWT_TYPE);
35         
36         jwtBuilder.setHeader(headerMap);
37         
38         String payload = "Hi my name is Sumanth";
39         jwtBuilder.setPayload(payload);
40         
41         String jwtString = jwtBuilder.compact();
42         return jwtString;
43     }
44     
45     public Jws toJWS(PublicKey publicKey, String data) {
46         JwtParserBuilder jwtParserBuilder = Jwts.parserBuilder();
47         jwtParserBuilder.setSigningKey(publicKey);
48         JwtParser jwtParser = jwtParserBuilder.build();
49         Jws jws = jwtParser.parsePlaintextJws(data);
50         return jws;
51     }
52 }

In the above code at line 19, I have created a key pair using “Keys” class static method “keyPairFor”.

The “Keys” class is provided by JJWT framework.

At line 21, I call “toJWTString” method passing the private key to the method.

The “toJWTString” method creates a JWT string signed by asymmetric key.

In the “toJWTString” method line 31, we set the received private key to “JwtBuilder” instance.

At line 41, using the “JwtBuilder” instance we create the JWT string.

Now this JWT string is signed by private key. It is returned to the “main” method.

At line 21 in the main method we assign the received jwt string to “data” variable.

At line 23, we pass the asymmetric key’s public key and jwt string to “toJWS” method to convert JWT in string format to an instance Jws class.

The function “toJWS” decrypt the JWT present in string format and convert to an instance of “Jws” class.

At line 47, we set the public key to an instance of “JwtParserBuilder”.

At line 49, we parse the JWT string contained in variable “data” by calling “parsePlaintextJws” method.

This will create an instance of Jws.

In this way we can encrypt and decrypt a JWT containing payload using asymmetric key.

Leave a Reply