@@ -56,9 +56,10 @@ static void encryptAndDecrypt(final String keyArn) {
5656 // also configure the credentials provider, client configuration and other settings as necessary
5757 final KmsClientSupplier clientSupplier = KmsClientSupplier .builder ().build ();
5858
59- // 3. Instantiate a KMS Keyring, supplying the keyArn as the generator for generating a data key.
60- // For this example, empty lists are provided for grant tokens and additional keys to encrypt the data
61- // key with, but those can be supplied as necessary.
59+ // 3. Instantiate a KMS Keyring, supplying the key ARN as the generator for generating a data key. While using
60+ // a key ARN is a best practice, for encryption operations it is also acceptable to use a CMK alias or an
61+ // alias ARN. For this example, empty lists are provided for grant tokens and additional keys to encrypt
62+ // the data key with, but those can be supplied as necessary.
6263 final Keyring keyring = StandardKeyrings .kms (clientSupplier , emptyList (), emptyList (), keyArn );
6364
6465 // 4. Create an encryption context
@@ -80,11 +81,12 @@ static void encryptAndDecrypt(final String keyArn) {
8081 final AwsCryptoResult <byte []> encryptResult = crypto .encryptData (config , EXAMPLE_DATA );
8182 final byte [] ciphertext = encryptResult .getResult ();
8283
83- // 7. Decrypt the data
84+ // 7. Decrypt the data. The same keyring may be used to encrypt and decrypt, but for decryption
85+ // the key IDs must be in the key ARN format.
8486 final AwsCryptoResult <byte []> decryptResult = crypto .decryptData (config , ciphertext );
8587
86- // 8. Before verifying the plaintext, verify that the key that was used in the encryption
87- // operation was the one used during the decryption operation .
88+ // 8. Before verifying the plaintext, inspect the Keyring Trace to verify that the CMK used
89+ // to decrypt the encrypted data key was the CMK in the encryption keyring .
8890 if (!decryptResult .getKeyringTrace ().getEntries ().get (0 ).getKeyName ().equals (keyArn )) {
8991 throw new IllegalStateException ("Wrong key ID!" );
9092 }
0 commit comments