@@ -81,17 +81,19 @@ public static void simpleAesKeyringReEncryptInstructionFile(
81
81
// Generate the original AES key for initial encryption
82
82
SecretKey originalAesKey = generateAesKey ();
83
83
84
+ // Sample metadata for AES keyring identification and context - not used for encryption/decryption purposes
85
+ // Helps distinguish between the old and new AES keyrings during the reEncryptInstructionFile operation
86
+ MaterialsDescription originalMaterialsDescription = MaterialsDescription
87
+ .builder ()
88
+ .put ("version" , "1.0" )
89
+ .put ("rotated" , "no" )
90
+ .build ();
91
+
84
92
// Create the original AES keyring with materials description
85
93
AesKeyring oldKeyring = AesKeyring
86
94
.builder ()
87
95
.wrappingKey (originalAesKey )
88
- .materialsDescription (
89
- MaterialsDescription
90
- .builder ()
91
- .put ("version" , "1.0" )
92
- .put ("rotated" , "no" )
93
- .build ()
94
- )
96
+ .materialsDescription (originalMaterialsDescription )
95
97
.build ();
96
98
97
99
// Create a default S3 client for instruction file operations
@@ -120,17 +122,19 @@ public static void simpleAesKeyringReEncryptInstructionFile(
120
122
// Generate a new AES key for re-encryption (rotating wrapping key)
121
123
SecretKey newAesKey = generateAesKey ();
122
124
125
+ // Sample metadata for rotated AES keyring identification and context - not used for encryption/decryption purposes
126
+ // Helps distinguish between the old and new AES keyrings during the reEncryptInstructionFile operation
127
+ MaterialsDescription newMaterialsDescription = MaterialsDescription
128
+ .builder ()
129
+ .put ("version" , "2.0" )
130
+ .put ("rotated" , "yes" )
131
+ .build ();
132
+
123
133
// Create a new keyring with the new AES key and updated materials description
124
134
AesKeyring newKeyring = AesKeyring
125
135
.builder ()
126
136
.wrappingKey (newAesKey )
127
- .materialsDescription (
128
- MaterialsDescription
129
- .builder ()
130
- .put ("version" , "2.0" )
131
- .put ("rotated" , "yes" )
132
- .build ()
133
- )
137
+ .materialsDescription (newMaterialsDescription )
134
138
.build ();
135
139
136
140
// Create the re-encryption of instruction file request to re-encrypt the encrypted data key with the new wrapping key
@@ -216,17 +220,19 @@ public static void simpleRsaKeyringReEncryptInstructionFile(
216
220
.privateKey (originalPrivateKey )
217
221
.build ();
218
222
223
+ // Sample metadata for RSA keyring identification and context - not used for encryption/decryption purposes
224
+ // Helps distinguish between the old and new RSA keyrings during the reEncryptInstructionFile operation
225
+ MaterialsDescription originalMaterialsDescription = MaterialsDescription
226
+ .builder ()
227
+ .put ("version" , "1.0" )
228
+ .put ("rotated" , "no" )
229
+ .build ();
230
+
219
231
// Create the original RSA keyring with materials description
220
232
RsaKeyring originalKeyring = RsaKeyring
221
233
.builder ()
222
234
.wrappingKeyPair (originalPartialRsaKeyPair )
223
- .materialsDescription (
224
- MaterialsDescription
225
- .builder ()
226
- .put ("version" , "1.0" )
227
- .put ("rotated" , "no" )
228
- .build ()
229
- )
235
+ .materialsDescription (originalMaterialsDescription )
230
236
.build ();
231
237
232
238
// Create a default S3 client for instruction file operations
@@ -264,17 +270,19 @@ public static void simpleRsaKeyringReEncryptInstructionFile(
264
270
.privateKey (newPrivateKey )
265
271
.build ();
266
272
273
+ // Sample metadata for rotated RSA keyring identification and context - not used for encryption/decryption purposes
274
+ // Helps distinguish between the old and new RSA keyrings during the reEncryptInstructionFile operation
275
+ MaterialsDescription newMaterialsDescription = MaterialsDescription
276
+ .builder ()
277
+ .put ("version" , "2.0" )
278
+ .put ("rotated" , "yes" )
279
+ .build ();
280
+
267
281
// Create the new RSA keyring with updated materials description
268
282
RsaKeyring newKeyring = RsaKeyring
269
283
.builder ()
270
284
.wrappingKeyPair (newPartialRsaKeyPair )
271
- .materialsDescription (
272
- MaterialsDescription
273
- .builder ()
274
- .put ("version" , "2.0" )
275
- .put ("rotated" , "yes" )
276
- .build ()
277
- )
285
+ .materialsDescription (newMaterialsDescription )
278
286
.build ();
279
287
280
288
// Create the re-encryption of instruction file request to re-encrypt the encrypted data key with the new wrapping key
@@ -334,7 +342,12 @@ public static void simpleRsaKeyringReEncryptInstructionFile(
334
342
335
343
/**
336
344
* This example demonstrates generating a custom instruction file to enable access to encrypted object by a third party.
337
- * This enables secure sharing of encrypted objects without sharing private keys.
345
+ * It showcases a scenario where:
346
+ * 1. The original client encrypts and uploads an object to S3.
347
+ * 2. The original client wants to share this encrypted object with a third party client without sharing their private key.
348
+ * 3. A new instruction file is created specifically for the third party client, containing the data key encrypted with the third party's public key.
349
+ * 4. The third party client can then access and decrypt the object using their own private key and custom instruction file.
350
+ * 5. The original client can still access and decrypt the object using their own private key and instruction file.
338
351
*
339
352
* @param bucket The name of the Amazon S3 bucket to perform operations on.
340
353
* @throws NoSuchAlgorithmException if RSA algorithm is not available
@@ -361,17 +374,19 @@ public static void simpleRsaKeyringReEncryptInstructionFileWithCustomSuffix(
361
374
.privateKey (clientPrivateKey )
362
375
.build ();
363
376
377
+ // Sample metadata for client keyring identification and context - not used for encryption/decryption purposes
378
+ // Helps distinguish between the client and third party RSA keyrings during the reEncryptInstructionFile operation
379
+ MaterialsDescription clientMaterialsDescription = MaterialsDescription
380
+ .builder ()
381
+ .put ("isOwner" , "yes" )
382
+ .put ("access-level" , "admin" )
383
+ .build ();
384
+
364
385
// Create the client's RSA keyring with materials description
365
386
RsaKeyring clientKeyring = RsaKeyring
366
387
.builder ()
367
388
.wrappingKeyPair (clientPartialRsaKeyPair )
368
- .materialsDescription (
369
- MaterialsDescription
370
- .builder ()
371
- .put ("isOwner" , "yes" )
372
- .put ("access-level" , "admin" )
373
- .build ()
374
- )
389
+ .materialsDescription (clientMaterialsDescription )
375
390
.build ();
376
391
377
392
// Create a default S3 client for instruction file operations
@@ -409,19 +424,21 @@ public static void simpleRsaKeyringReEncryptInstructionFileWithCustomSuffix(
409
424
.privateKey (thirdPartyPrivateKey )
410
425
.build ();
411
426
427
+ // Sample metadata for third party keyring identification and context - not used for encryption/decryption purposes
428
+ // Helps distinguish between the client and third party RSA keyrings during the reEncryptInstructionFile operation
429
+ MaterialsDescription thirdPartyMaterialsDescription = MaterialsDescription
430
+ .builder ()
431
+ .put ("isOwner" , "no" )
432
+ .put ("access-level" , "user" )
433
+ .build ();
434
+
412
435
// Create RSA keyring with third party's public key and updated materials description for re-encryption request
413
436
RsaKeyring sharedKeyring = RsaKeyring
414
437
.builder ()
415
438
.wrappingKeyPair (
416
439
PartialRsaKeyPair .builder ().publicKey (thirdPartyPublicKey ).build ()
417
440
)
418
- .materialsDescription (
419
- MaterialsDescription
420
- .builder ()
421
- .put ("isOwner" , "no" )
422
- .put ("access-level" , "user" )
423
- .build ()
424
- )
441
+ .materialsDescription (thirdPartyMaterialsDescription )
425
442
.build ();
426
443
427
444
// Create RSA keyring with third party's public and private keys for decryption purposes with updated materials description
0 commit comments