I'm trying to create a nested json JWT following the example provided over here http://connect2id.com/products/nimbus-jose-jwt/examples/signed-and-encrypted-jwt
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.*;
import com.nimbusds.jwt.*;
// Generate 256-bit AES key for HMAC as well as encryption
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
// Create HMAC signer
JWSSigner signer = new MACSigner(secretKey.getEncoded());
// Prepare JWT with claims set
JWTClaimsSet claimsSet = new JWTClaimsSet();
claimsSet.setSubject("alice");
claimsSet.setIssueTime(new Date());
claimsSet.setIssuer("https://c2id.com");
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
// Apply the HMAC
signedJWT.sign(signer);
// Create JWE object with signed JWT as payload
JWEObject jweObject = new JWEObject(
new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A256GCM)
.contentType("JWT") // required to signal nested JWT
.build(),
new Payload(signedJWT));
// Perform encryption
jweObject.encrypt(new DirectEncrypter(secretKey.getEncoded()));
// Serialise to JWE compact form
String jweString = jweObject.serialize();
The problem I'm having the resultant jweString is not coming correctly
eyJhbGc.
.6Ne
.tw8z
I'm getting 4 parts instead of 3 parts and there is one empty part
eyJhbGc.
.6Ne
The general format of a JWE has five parts, <Header>.<EncryptedKey>.<InitializationVector>.<Ciphertext>.<AuthenticationTag>. In some cases, like using direct encryption that is in the example, there is no encrypted key and that part is left empty. So you should be getting a JWE with five parts where one part is empty that looks something like, eyJjdHkiOiJKV1QiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiZGlyIn0..PVEd_V4E8qyppt6u.UOLp2qTNMw3iA9tyTEDHeJAdTczzI5uP4BkXHQSC6U73kywATBbvdWqz9nilsSWr1y-E1duTMvRL7hzDyyWPMZnpfnCWsjhzZtTnd22HODMWUpU.CoQq987RvHGIN5D6HhrjiQ
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With