18
18
import com .amazonaws .encryptionsdk .EncryptRequest ;
19
19
import com .amazonaws .encryptionsdk .keyrings .Keyring ;
20
20
import com .amazonaws .encryptionsdk .keyrings .StandardKeyrings ;
21
- import com .amazonaws .encryptionsdk .kms .AwsKmsClientSupplier ;
22
21
23
22
import java .nio .charset .StandardCharsets ;
24
23
import java .security .GeneralSecurityException ;
28
27
import java .security .PublicKey ;
29
28
import java .util .Arrays ;
30
29
31
- import static java .util .Collections .emptyList ;
32
-
33
30
/**
34
31
* <p>
35
32
* Encrypts data using both KMS and an asymmetric key pair.
@@ -75,7 +72,7 @@ static void escrowEncryptAndDecrypt(String kmsArn) throws GeneralSecurityExcepti
75
72
byte [] standardDecryptedData = standardDecrypt (kmsArn , encryptedData );
76
73
77
74
// Decrypt the data using the escrowed RSA Key
78
- byte [] escrowedDecryptedData = escrowDecrypt (encryptedData , escrowKeyPair .getPublic (), escrowKeyPair . getPrivate ());
75
+ byte [] escrowedDecryptedData = escrowDecrypt (encryptedData , escrowKeyPair .getPrivate ());
79
76
80
77
// Verify both decrypted data instances are the same as the original plaintext
81
78
assert Arrays .equals (standardDecryptedData , EXAMPLE_DATA );
@@ -88,25 +85,23 @@ private static byte[] standardEncrypt(final String kmsArn, final PublicKey publi
88
85
// 1. Instantiate the SDK
89
86
final AwsCrypto crypto = new AwsCrypto ();
90
87
91
- // 2. Instantiate an AWS KMS Client Supplier. This example uses the default client supplier but you can
92
- // also configure the credentials provider, client configuration and other settings as necessary
93
- final AwsKmsClientSupplier clientSupplier = AwsKmsClientSupplier .builder ().build ();
94
-
95
- // 3. Instantiate an AWS KMS Keyring, supplying the keyArn as the generator for generating a data key.
96
- // For this example, empty lists are provided for grant tokens and additional keys to encrypt the data
97
- // key with, but those can be supplied as necessary.
98
- final Keyring kmsKeyring = StandardKeyrings .awsKms (clientSupplier , emptyList (), emptyList (), kmsArn );
88
+ // 2. Instantiate an AWS KMS Keyring, supplying the keyArn as the generator for generating a data key.
89
+ final Keyring kmsKeyring = StandardKeyrings .awsKms (kmsArn );
99
90
100
- // 4 . Instantiate a RawRsaKeyring
91
+ // 3 . Instantiate a RawRsaKeyring
101
92
// Because the user does not have access to the private escrow key,
102
93
// they pass in "null" for the private key parameter.
103
- final Keyring rsaKeyring = StandardKeyrings .rawRsa ("Escrow" , "Escrow" ,
104
- publicEscrowKey , null , "RSA/ECB/OAEPWithSHA-512AndMGF1Padding" );
105
-
106
- // 5. Combine the providers into a single MultiKeyring
94
+ final Keyring rsaKeyring = StandardKeyrings .rawRsa ()
95
+ .keyNamespace ("Escrow" )
96
+ .keyName ("Escrow" )
97
+ .publicKey (publicEscrowKey )
98
+ .wrappingAlgorithm ("RSA/ECB/OAEPWithSHA-512AndMGF1Padding" )
99
+ .build ();
100
+
101
+ // 4. Combine the providers into a single MultiKeyring
107
102
final Keyring keyring = StandardKeyrings .multi (kmsKeyring , rsaKeyring );
108
103
109
- // 6 . Encrypt the data with the keyring.
104
+ // 5 . Encrypt the data with the keyring.
110
105
// To simplify the code, we omit the encryption context. Production code should always
111
106
// use an encryption context. For an example, see the other SDK samples.
112
107
return crypto .encrypt (EncryptRequest .builder ()
@@ -121,14 +116,8 @@ private static byte[] standardDecrypt(final String kmsArn, final byte[] cipherTe
121
116
// 1. Instantiate the SDK
122
117
final AwsCrypto crypto = new AwsCrypto ();
123
118
124
- // 2. Instantiate an AWS KMS Client Supplier. This example uses the default client supplier but you can
125
- // also configure the credentials provider, client configuration and other settings as necessary
126
- final AwsKmsClientSupplier clientSupplier = AwsKmsClientSupplier .builder ().build ();
127
-
128
- // 3. Instantiate an AWS KMS Keyring, supplying the keyArn as the generator for generating a data key.
129
- // For this example, empty lists are provided for grant tokens and additional keys to encrypt the data
130
- // key with, but those can be supplied as necessary.
131
- final Keyring kmsKeyring = StandardKeyrings .awsKms (clientSupplier , emptyList (), emptyList (), kmsArn );
119
+ // 2. Instantiate an AWS KMS Keyring, supplying the keyArn as the generator for generating a data key.
120
+ final Keyring kmsKeyring = StandardKeyrings .awsKms (kmsArn );
132
121
133
122
// 4. Decrypt the data with the keyring.
134
123
// To simplify the code, we omit the encryption context. Production code should always
@@ -138,16 +127,20 @@ private static byte[] standardDecrypt(final String kmsArn, final byte[] cipherTe
138
127
.ciphertext (cipherText ).build ()).getResult ();
139
128
}
140
129
141
- private static byte [] escrowDecrypt (final byte [] cipherText , final PublicKey publicEscrowKey , final PrivateKey privateEscrowKey ) {
130
+ private static byte [] escrowDecrypt (final byte [] cipherText , final PrivateKey privateEscrowKey ) {
142
131
// You can decrypt the stream using only the private key.
143
132
// This method does not call KMS.
144
133
145
134
// 1. Instantiate the SDK
146
135
final AwsCrypto crypto = new AwsCrypto ();
147
136
148
137
// 2. Instantiate a RawRsaKeyring using the escrowed private key
149
- final Keyring rsaKeyring = StandardKeyrings .rawRsa ("Escrow" , "Escrow" ,
150
- publicEscrowKey , privateEscrowKey , "RSA/ECB/OAEPWithSHA-512AndMGF1Padding" );
138
+ final Keyring rsaKeyring = StandardKeyrings .rawRsa ()
139
+ .keyNamespace ("Escrow" )
140
+ .keyName ("Escrow" )
141
+ .privateKey (privateEscrowKey )
142
+ .wrappingAlgorithm ("RSA/ECB/OAEPWithSHA-512AndMGF1Padding" )
143
+ .build ();
151
144
152
145
// 3. Decrypt the data with the keyring
153
146
// To simplify the code, we omit the encryption context. Production code should always
0 commit comments