@@ -56,9 +56,10 @@ static void encryptAndDecrypt(final String keyArn) {
56
56
// also configure the credentials provider, client configuration and other settings as necessary
57
57
final KmsClientSupplier clientSupplier = KmsClientSupplier .builder ().build ();
58
58
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.
62
63
final Keyring keyring = StandardKeyrings .kms (clientSupplier , emptyList (), emptyList (), keyArn );
63
64
64
65
// 4. Create an encryption context
@@ -80,11 +81,12 @@ static void encryptAndDecrypt(final String keyArn) {
80
81
final AwsCryptoResult <byte []> encryptResult = crypto .encryptData (config , EXAMPLE_DATA );
81
82
final byte [] ciphertext = encryptResult .getResult ();
82
83
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.
84
86
final AwsCryptoResult <byte []> decryptResult = crypto .decryptData (config , ciphertext );
85
87
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 .
88
90
if (!decryptResult .getKeyringTrace ().getEntries ().get (0 ).getKeyName ().equals (keyArn )) {
89
91
throw new IllegalStateException ("Wrong key ID!" );
90
92
}
0 commit comments