...
Code Block | ||||
---|---|---|---|---|
| ||||
final String text = "The true sign of intelligence is not knowledge but imagination."; // Create the secret keys for encrypting the content encryption key: SecretKey wrapperKey1 = CryptoUtils.createSecretKeySpec(WRAPPER_BYTES1, "AES"); SecretKey wrapperKey2 = CryptoUtils.createSecretKeySpec(WRAPPER_BYTES2, "AES"); // Create KeyEncryptionProviders initialized with these secret keys: JweHeadersKeyEncryptionProvider protectedHeaderskeyEncryption1 = new JweHeaders(ContentAlgorithm.A128GCMJweUtils.getSecretKeyEncryptionAlgorithm(wrapperKey1, KeyAlgorithm.A128KW); JweHeadersKeyEncryptionProvider sharedUnprotectedHeaderskeyEncryption2 = new JweHeaders(); sharedUnprotectedHeaders.setJsonWebKeysUrl("https://server.example.com/keys.jwks"); sharedUnprotectedHeaders.setKeyEncryptionAlgorithm(JweUtils.getSecretKeyEncryptionAlgorithm(wrapperKey2, KeyAlgorithm.A128KW); // Create ContentEncryptionProvider: ContentEncryptionProvider contentEncryption = new AesGcmContentEncryptionAlgorithm(CEK_BYTES, ContentAlgorithm.A128GCM); // ifIf a single recipient then this line is enough: //ContentEncryptionProvider contentEncryption = JweUtils.getContentEncryptionProvider(ContentAlgorithm.A128GCM); // Prepare JweEncryptionProviders, one per each recipient. List<JweEncryptionProvider> jweProviders = new LinkedList<JweEncryptionProvider>(); jweProviders.add(new JweEncryption(keyEncryption1, contentEncryption)); jweProviders.add(new JweEncryption(keyEncryption2, contentEncryption)); // Let the recipients know that the key encryption algorithm is A128KW. // This step is optional if the recipients support A128KW only. // Note because these headers are shared A128KW needs to be supported by all the recipients. // Per-reciepient specific headers can be used instead to note the key encryption algorithm if required. // One can also consider setting this property in the shared protected headers, same as it is done below // with the content algorithm JweHeaders sharedUnprotectedHeaders = new JweHeaders(); sharedUnprotectedHeaders.setKeyEncryptionAlgorithm(KeyAlgorithm.A128KW); // Set some other custom shared unprotected header sharedUnprotectedHeaders.setHeader("customHeader", "customValue"); // Let the recipients know that the content KeyEncryptionProviderencryption keyEncryption1algorithm =is JweUtilsA128GCM.getSecretKeyEncryptionAlgorithm(wrapperKey1, KeyAlgorithm.A128KW); JweEncryptionProvider jweEnc1 = new JweEncryption(keyEncryption1, contentEncryption); KeyEncryptionProvider keyEncryption2 = JweUtils.getSecretKeyEncryptionAlgorithm(wrapperKey2, KeyAlgorithm.A128KW); JweEncryptionProvider jweEnc2 // This step is optional if the recipients support A128GCM only. JweHeaders protectedHeaders = new JweEncryption(keyEncryption2, contentEncryptionJweHeaders(ContentAlgorithm.A128GCM); List<JweEncryptionProvider>// jweProvidersSet =per-recipient new LinkedList<JweEncryptionProvider>(); jweProviders.add(jweEnc1); jweProviders.add(jweEnc2); specific headers List<JweHeaders> perRecipientHeades = new LinkedList<JweHeaders>(); perRecipientHeades.add(new JweHeaders("key1")); perRecipientHeades.add(new JweHeaders("key2")); JweJsonProducer p = new JweJsonProducer(protectedHeaders, sharedUnprotectedHeaders, StringUtils.toBytesUTF8(text), StringUtils.toBytesUTF8(EXTRA_AAD_SOURCE), false); String jweJsonOut = p.encryptWith(jweProviders, perRecipientHeades); JweJsonConsumer consumer = new JweJsonConsumer(jweJsonOut); KeyAlgorithm keyAlgo = consumer.getSharedUnprotectedHeader().getKeyEncryptionAlgorithm(); ContentAlgorithm ctAlgo = consumer.getProtectedHeader().getContentEncryptionAlgorithm(); // first recipient: JweDecryptionProvider jwe1 = JweUtils.createJweDecryptionProvider(wrapperKey1, keyAlgo, ctAlgo); // the consumer will iterate over JWE entries and will try to find the one which can be decrypted with this decryptor // or do consumer.getRecipientsMap() returning a list of entries and their metadata to do a more precise selection. String content = consumer.decryptWith(jwe1, Collections.singletonMap("kid", "key1")).getContent(); // second recipient: JweDecryptionProvider jwe2 = JweUtils.createJweDecryptionProvider(wrapperKey2, keyAlgo, ctAlgo); content = consumer.decryptWith(jwe2, Collections.singletonMap("kid", "key1key2")).getContent(); |
If the sequence contains a single recipient entry only then the JWE JSON 'recipients' array will contain a single entry, or the whole sequence can be flattened instead with the actual 'recipients' array dropped. JweJsonProducer does not produce the flattened sequence when only a single encryption is done by default because 3rd party JWE JSON consumers may only be able to process the sequences with the 'recipients' array, so pass a 'canBeFlat' flag to JwEJsonProducer if needed
...