0

I'm using java code to generate the JWE token using nimbus jar file but I'm unable to generate expected header JWE token, could you guys please help me here to generate the expected token.

static String  PUBLIC_KEY ="askjdbaskjbdjkasbkjdbaskjbdasjkbdkjsabdjkaskjdbaskjdbaskjbd";
public static void main(){
    JSONObject obj = new JSONObject();
        obj.put("typ", "JWT");
        obj.put("cty", "JWT");
        obj.put("enc", "A128CBC-HS256");
        obj.put("alg", "RSA-OAEP");
    
        
//      JWEHeader head = (JWEHeader) Header.parse(JSON);
        JWEHeader header = JWEHeader.parse(obj);
        System.out.println(header.getContentType());
        JWEObject jwe = new JWEObject(header,new Payload("Hello, world!"));
        EncryptionMethod enc = EncryptionMethod.A128CBC_HS256;
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(enc.cekBitLength());
        SecretKey cek = keyGenerator.generateKey();
            jwe.encrypt(new RSAEncrypter(getPublicKey(PUBLIC_KEY),cek));
            String jweString = jwe.serialize();
            System.out.println(jweString);
}

While I'm generating the token I'm getting token like this:

eyJjdHkiOiJKV1QiLCJ0eXAiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAifQ.ITv2i07ctBtWR8T6D63yyHE7QXIw5paeYpLYYGjN2wx9Pq5IBiYxWJBNoynZyCSC-MSFiTl55EOs1mB1qCjz0z2f25DFsnDocHJ7xNfmL5_slRCpdJX-InTcVm4NSnU2iVxaxAP5ODFrlJIgLCqICMq0MaAT8wSXAkaoVJx6MH9MRmhJzsdxCvoZaj5lu83HSL_FEveVvXt4Bk3Zq9DipVPlcbog5t1I8We8tscMW6QsOM47xxIQxmQscHOkufFkMCQOUi-LwFu4k7f6vIcASc3jbJ9VwGCPehIef0PjwQPxiO7W8tmKjcIeAF6ytYUC2nEPUwpsMZVNIaGcNPSU-w.22YugdfNWVlHWJS3OR8aiQ.S1anurm9Dim2-lV5QbNvAA.Dvfn31gFhqKbkoU3ZVGNXQ 

but header is generating like this, but I don't want this type of header in the JWE Token:

eyJjdHkiOiJKV1QiLCJ0eXAiOiJKV1QiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiUlNBLU9BRVAifQ

I want to generate a header like this using the above java code:

eyJ0eXAiOiJKV1QiLCJjdHkiOiJKV1QiLCJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhDQkMtSFMyNTYifQ

Could you guys please help me here to generate header like above one.

Thanks.

jps
  • 20,041
  • 15
  • 75
  • 79
  • 1
    *I don't want this type of header* *I want to generate like this header* - the examples you hav shown are basically identical, just a different order of the keye in JSON, but that should not matter. What's the problem? – jps Jul 07 '21 at 11:31
  • Your generated **token** starts with the requested **header**, so simply split the token on "." and take the first string/part. – Michael Fehr Jul 07 '21 at 12:32
  • Hi I couldn't understand the answer can you guys write a snippet of code and give to me it was so helpful – Abhinav kiran Jul 07 '21 at 13:43
  • What I am saying is, the headers are almost identical: 1. example `{ "cty": "JWT", "typ": "JWT", "enc": "A128CBC-HS256", "alg": "RSA-OAEP" }` 2. example : `{ "typ": "JWT", "cty": "JWT", "alg": "RSA-OAEP", "enc": "A128CBC-HS256" }` - the keyes and values are identical, just in different order. The order of key/value pairs in JSON should be no problem. – jps Jul 07 '21 at 13:48
  • here is a related Q/A about the order of key/values pairs in a JSON: https://stackoverflow.com/questions/16870416/does-the-sequence-of-the-values-matter-in-a-json-object – jps Jul 07 '21 at 14:47
  • what I see is based upon the order also header of value is changing like the above can you check the code and tell me about this? – Abhinav kiran Jul 08 '21 at 05:21
  • sorry, I did not understand your last comment. What does "also header of value is changing" mean? – jps Jul 08 '21 at 07:58
  • what I'm trying to say is when in nimbus JWE token generation In any order you assign alg,enc,cty,typ in headers it taking some default order to generate the JWE header like this { "cty": "JWT", "typ": "JWT", "enc": "A128CBC-HS256", "alg": "RSA-OAEP" } – Abhinav kiran Jul 08 '21 at 13:44

0 Answers0